Search This Blog

Showing posts with label battery. Show all posts
Showing posts with label battery. Show all posts

Thursday, June 7, 2012

How to get the battery level And the phone number of the phone

For the bettery level of the phone we need to write the following code.


private void batteryLevel() {
             BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
                       public void onReceive(Context context, Intent intent) {
                                    context.unregisterReceiver(this);
                                    int rawlevel = intent.getIntExtra("level", -1);
                                    int scale = intent.getIntExtra("scale", -1);
                                    int level = -1;
                                    if (rawlevel >= 0 && scale > 0) {
                                                       level = (rawlevel * 100) / scale;
                                    }
             batterLevel.setText("Battery Level Remaining: " + level + "%");
         }
        };
   IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
   registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}



Here level indicates the bettery level of the phone.


For getting the phone number of the phone we have to insert the following code...


TelephonyManager phoneManager = (TelephonyManager) getApplicationContext()                            
                                                                     .getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();

Here phoneNumber indicates the number of the current phone.