Monday, July 9, 2012

Get Screen Brightness

In general the screen brightness varies in between 0 to 255. The below snippet helps to find the screen brightness.

public static int getScreenBrightness(Context context)
{
          try
          {
              return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
          }
          catch (SettingNotFoundException e)
          {
              Log.e("getScreenBrightness()", ""+e.getMessage());
              return -1;
          }
}

We have to pass context of the activity from where we are calling this method.

This method will return screen brightness as a result.

Getting WI-FI strength in percentage

The below snippet helps to find the WI-FI strength in the percentage.

public static int getWifiStrength(Context
{
          try
          {
              WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
              int rssi = wifiManager.getConnectionInfo().getRssi();
              int level = WifiManager.calculateSignalLevel(rssi, 10);
              int percentage = (int) ((level/10.0)*100);
              return percentage;
          }
          catch (Exception e)
          {
              return 0;
          }
}


We have to pass context of the activity from where we are calling this method.

It will return WI-FI strength in percentage. In case if any exception found then it will return 0;

Get Application version based on package name

The below snippet helps to find the application version

public static String getVersionName(Context context, String strPkgName)
{
         try
         {
            return context.getPackageManager().getPackageInfo(strPkgName, 0).versionName;
         }
         catch (NameNotFoundException e)
         {
            Log.e("getVersionName()", ""+e.getMessage());
            return "";
         }
}

Here we have to pass context of the application and package name of the application that you want to find the version.

If the application exists(Installed) in the mobile then it will return the application version as string. otherwise it will return empty string.