Saturday, May 5, 2012

Check battery usage, with Intent.ACTION_POWER_USAGE_SUMMARY


Check battery usage, with Intent.ACTION_POWER_USAGE_SUMMARY

To show power usage information to the user, you can start activity using Intent.ACTION_POWER_USAGE_SUMMARY.

Check battery usage, with Intent.ACTION_POWER_USAGE_SUMMARY

package com.exercise.IntentBatteryUsage;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidIntentBatteryUsageActivity extends Activity {
 
 Button btnCheckBattery;
 Intent intentBatteryUsage;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnCheckBattery = (Button)findViewById(R.id.checkBattery);
        
        intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
        ResolveInfo resolveInfo = getPackageManager().resolveActivity(intentBatteryUsage,0);
        
        if(resolveInfo == null){
         Toast.makeText(AndroidIntentBatteryUsageActivity.this, 
           "Not Support!", 
           Toast.LENGTH_LONG).show();
         btnCheckBattery.setEnabled(false);
        }else{
         btnCheckBattery.setEnabled(true);
        }
        
        btnCheckBattery.setOnClickListener(new Button.OnClickListener(){

   public void onClick(View v) {
    // TODO Auto-generated method stub
    startActivity(intentBatteryUsage);
   }});
   
    }
}

No comments: