Create “Battery usage” intent android

2019-01-25 13:00发布

问题:

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

回答1:

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);
}


回答2:

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();
}