-2

I want to develop an android app which Monitoring the Battery Level and Charging State as specified in http://developer.android.com/training/monitoring-device-state/battery-monitoring.html#CurrentLevel

However, I am not sure where to start with and where to put the intent filters and broadcast receivers mentioned in this tutorial.

Please help as how to start with the intent filter mentioned as:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
1

2 Answers 2

0

Use following code

import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView batteryPercent;
private void getBatteryPercentage() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
         context.unregisterReceiver(this);
         int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
         int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
         int level = -1;
         if (currentLevel >= 0 && scale > 0) {
             level = (currentLevel * 100) / scale;
         }
         batteryPercent.setText("Battery Level Remaining: " + level + "%");
     }
 }; 
 IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

/* @Override*/
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     batteryPercent = (TextView) this.findViewById(R.id.textview);
     getBatteryPercentage();
}

//    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

use following code Check if device is plugged in

public static boolean isPlugged(Context context) {
boolean isPlugged= false;
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
if (VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN) {
    isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
return isPlugged;
}
4
  • Thanks a lot, I tried and it gives me the status as 95%..... Now I want to add the next part as How extract both the current charging status and, if the device is being charged, whether it's charging via USB or AC charger: Can you please extend some help on this
    – Govind
    Oct 29, 2015 at 8:52
  • Thanks a lot, I tried and it gives me the status as 95%..... Now I want to add the next part as How extract both the current charging status and, if the device is being charged, whether it's charging via USB or AC charger: Can you please extend some help on this
    – Govind
    Oct 29, 2015 at 8:55
  • check my upadate answer
    – sasikumar
    Oct 29, 2015 at 9:05
  • I tried the above code. The code runs without error however its not displayed on the virtual machines screen. I am sorry in case I am asking basic questions however I am new to these things and trying to learn from basics. Please see below my MainActivity.java code for your kind reference.
    – Govind
    Oct 30, 2015 at 5:28
0
package com.example.govind.batterystate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView batteryPercent;
    private void getBatteryPercentage() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (currentLevel >= 0 && scale > 0) {
                    level = (currentLevel * 100) / scale;
                }
                batteryPercent.setText("Status: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        batteryPercent = (TextView) this.findViewById(R.id.statusValue);
        getBatteryPercentage();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    //Plugged In or Not

    public static boolean isPlugged(Context context) {
        boolean isPlugged= false;
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
        //if (VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN) {
          //  isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
        //}
        return isPlugged;


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

Virtual Machine's screen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.