Get battery level using BroadcastReceiver in Android service

I have the code I need to get the battery level of my Android device, but I have a big problem! I have an Android service where I get the charge level and send UDP.

This is my Service Code:

public class Servico extends Service {

String bateria = "Nada";

/*
 * Recupera nรญvel de bateria
 */
private BroadcastReceiver BatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        bateria = String.valueOf(level);
    }
};

public void onCreate() {
    super.onCreate();
    this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(getApplicationContext(), "Command=" + bateria + "%", Toast.LENGTH_LONG).show();
    stopSelf();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(BatInfoReceiver);
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

      

}

My problem: I can't get the level! I put BreakPoint in the onReceive method in BatInfoReceiver, but this code is only executed after the onStartCommand is executed, and e NEED uses the battery level value in the onStartCommand.

How can i do this?

obs: I apologize for possible mistakes in English. But I try not to use google translator.

+3


source to share


3 answers


take a look at this, it will give you the battery level you need to start servicing. Just use BatteryManager.EXTRA_LEVEL

as a key to gain level from intent



http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

+1


source


You have added the following permission to your main file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

      

and change the onReceived method to:



private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive( Context context, Intent intent )
            {
                int level = intent.getIntExtra( "level", 0 );
                bat= String.valueOf(level) + "%" ;
            }
    };

      

and to get the battle level use this line of code:

registerReceiver( batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED) );

      

+2


source


The solution to my problem was tyczj's answer ... I just adjusted to my problem.

My new code:

public void onCreate() {
    super.onCreate();

    //registerReceiver(BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

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

    // Are we charging / charged?
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    bateria = String.valueOf(status);

}

      

I am very grateful to everyone for their help! Stay of God! Thank!!

+1


source







All Articles