Android Context Get getBatteryUsageInfo(Context context)

Here you can find the source of getBatteryUsageInfo(Context context)

Description

Gets the battery info of device, and then writes it to a file.

Parameter

Parameter Description
context the context of application

Declaration

public static String getBatteryUsageInfo(Context context) 

Method Source Code

//package com.java2s;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class Main {
    /**//from w  w w  .  j ava 2  s  .  co  m
     * Gets the battery info of device, and then writes it to a file.
     * 
     * @param context the context of application
     */
    public static String getBatteryUsageInfo(Context context) {
        Intent batteryIntent = context.getApplicationContext()
                .registerReceiver(null,
                        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int rawlevel = batteryIntent.getIntExtra("level", -1);
        double scale = batteryIntent.getIntExtra("scale", -1);
        return String.format("Device has %s of %s battery left", rawlevel,
                scale);
    }
}

Related

  1. getApplicationMetaData(Context context, String key)
  2. getApplicationName(Context ctx)
  3. getAsset(@Nonnull Context context, @Nonnull String path)
  4. getAsset(@Nonnull Context context, @Nonnull String path)
  5. getAuthorityFromPermission(Context context, String permission)
  6. getBoolean(Context context, String key, boolean defValue)
  7. getBroadcastAddress(Context context)
  8. getCarrier(Context context)
  9. getCertificateFingerprint(Context ctx, String packageName)