9

On my nexus one, there is a handy app reachable from Settings > About Phone > Battery use.

I'd like to StartActivity() that app from one of my Activities.

I can see in the log that when Settings runs it, this intent is logged:

Starting activity:
  Intent { act=android.intent.action.MAIN
           cmp=com.android.settings/.fuelgauge.PowerUsageSummary }

I'm having trouble relating that to something in Android Java source. I can't even find "fuelgauge" in the GIT source. Can anyone point me to the right file, or anything else helpful, like how to create the right kind of Intent?

Thanks

Peter

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Peter vdL
  • 4,953
  • 10
  • 40
  • 60

2 Answers2

23

Code is as follows:

Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
if(resolveInfo != null){
    startActivity(powerUsageIntent);
}
Chris Lacy
  • 4,222
  • 3
  • 35
  • 33
  • Is it also possible to go to the battery stats of a single app? Also, do you know how to get to the stats of the mobile data usage? – android developer Apr 26 '14 at 15:09
  • Launching the Activity that displays battery stats and fetching an app's battery/data usage are very different questions. I think you should post new questions. – Chris Lacy Apr 28 '14 at 05:48
  • They are, kinda: http://stackoverflow.com/questions/23312038/how-to-get-battery-mobile-data-usage-of-specific-apps . I wanted to know how to get those stats via the app, and when I didn't find sufficient information, I asked (on the comments) how to get the intents for those stuff. – android developer Apr 28 '14 at 06:44
0

Base on the handful code of @Chris Lacy , I wrapped the code to static method that you call to open this screen:

public static void openBatteryUsagePage(Context ctx){
    Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
    ResolveInfo resolveInfo = ctx.getPackageManager().resolveActivity(powerUsageIntent, 0);
    // check that the Battery app exists on this device
    if(resolveInfo != null){
        ctx.startActivity(powerUsageIntent);
    } else
        Toast.makeText(ctx, R.string.not_found, Toast.LENGTH_LONG).show();
} 
yshahak
  • 4,996
  • 1
  • 31
  • 37