Saturday, December 21, 2013

Android Get Current Battery Level

Get current battery level once on Android platform by using Battery_Changed Intent. This gets total scale value and current level to calculate.

public float getCurrentBatteryLevel() {
        Intent battery = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        
        int batteryLevel = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        
        int batteryScale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        //to avoid error use this
        if(batteryLevel == -1 || batteryScale == -1) {            
                return 50.0f;
        }

        return ((float)batteryLevel / (float)batteryScale) * 100.0f;
    }

Android - Turn WiFi Hotspot On and Off Programmatically


Programmatically turn WiFi Hotspot On and Off on android platform using following permissions in Manifest file.

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE

and following code

WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

WifiConfiguration wifi_config = null;

wifi.setWifiEnabled(false); //True or False

//There is no direct call so use following... //USE REFLECTION ON THE METHOD NAMED //SetWifiAPEnabled//
Method methodAP = wifi.getClass().getMethod("setWifiApEnabled", WifiConfig.class, boolean.class);

methodAP.invoke(wifi, wifi_config, false); //True or False


    

Android - Turn WiFi On and Off Programmatically


Programmatically turn WiFi On and Off on android platform using following permissions in Manifest file.

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE

and following code

WifiManager wifi_manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifi_manager.setWifiEnabled(false); //True or False


    

Tuesday, December 25, 2012

cordova 2.2.0 messages.slice error in eclipse (android)

When using cordova 2.2.0 in eclipse for android development if you see following error

TypeError: 'undefined' is not a function (evaluating 'messages.slice(0, spaceIdx)') at file:///android_asset/www/cordova-2.2.0.js:1113

No need to worry, just do the following

Change line 1101

messages = messagesFromNative.shift();
to
messages = messagesFromNative.shift() + '';


Wednesday, September 28, 2011

Dynamically Calculate Control Dimension at UI

Calculate the dimension of controls at UI in term of rows*columns. This accepts parent container width/height and number of controls to draw. You may also provide what ratio you need for controls.


/// Calculate control dimension as per parent size and required ratio
/// param name="ParentWidth">Parent control width
/// param name="ParentHeight">Parent control height
/// param name="TotalControls">Total control
/// param name="RatioX">X ratio of control to maintain
/// param name="RatioY">Y ratio of control to maintain
/// param name="TotalColumns">get columns
/// param name="TotalRows">get rows
public void CalculateDimensions(int ParentWidth, int ParentHeight, int TotalControls, int RatioX, int RatioY, out int TotalColumns, out int TotalRows)
{
TotalColumns = TotalRows = 1;

      double areaRatio = Math.Sqrt(((double)(ParentHeight * RatioX) / (double)(ParentWidth * RatioY)) / (double)TotalControls);
      double expectedWidth = areaRatio * ParentWidth;
      double expectedHeight = areaRatio * ParentHeight;

      if (ParentWidth > ParentHeight)
            TotalColumns = (int)Math.Ceiling((double)ParentWidth / (double)expectedWidth);
      else
            TotalRows = (int)Math.Ceiling((double)ParentHeight / (double)expectedHeight);

bool isOK = false;
      while (!isOK)
      {
            if (ParentWidth > ParentHeight)
            {
                  TotalRows = (int)(Math.Ceiling((double)TotalControls / (double)TotalColumns));
                  if (TotalColumns >= TotalRows) isOK = true; else TotalColumns++;
            }
            else
            {
                  TotalColumns = (int)(Math.Ceiling((double)TotalControls / (double)TotalRows));
                  if (TotalRows >= TotalColumns) isOK = true; else TotalRows++;
            }
       }
}

Thursday, June 30, 2011

C# Shortcuts (Automatic Properties)


C # 3.0 introduces automatic properties. A property is usually (but not have to) to a private variable that is exposed to the outside world through getters and setters. The following is a common example of this



public class Employee
{
 private string _fName;
 public string FName
 {
    get { return _fName; }
    set { _fName = value; }
 }
}
Now see the magic...
public class Employee
{
 public string FName { get; set; }
}
C # compiler automatically creates a variable background and the right to get and set properties. Why is it useful? After all, you could have just done a string variable instead of a public class.

When you define as a property allows you to add validation logic in the current class at a later stage. The signature in the memory of the class will not change which means that any external library compiled code need not be recompiled

C# Shortcuts (Nullable objects)

The variable must have a value, can not be empty. Sometimes it would be convenient, it was possible to give a "null" (for example, undefined) variable. . NET 2.0 Nullable general use, that makes this possible. The next two lines to produce exactly the same purpose:


Nullable myVar = null;
or use this...
int? myVar = null;
By one ? According to a definition of the variable, the compiler will wrap a Nullable  generic type.