如何检测充电日志的电池电量和状态?

时间:2014-02-19 08:09:53

标签: android status batterylevel

我正在尝试为Android中的电池充电日志,它工作正常。但问题是我无法检测电池电量以及充电器的状态。为了您的信息,我是一个使用Android的初学者,希望有人可以帮助我使用代码。

    public class ListViewA extends Activity {
/**Called when the activity is first created.*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    ListView lv= (ListView)findViewById(R.id.listview);

     // create the grid item mapping
    String[] from = new String[] {"col_1", "col_2", "col_3", "col_4"};
    int[] to = new int[] { R.id.item2, R.id.item3, R.id.item4, R.id.item5};

    // prepare the list of all records
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();        

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
     //get current date time with Calendar()
    Calendar cal = Calendar.getInstance();

         BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {  
           public void onReceive(Context context, Intent intent) {  
                context.unregisterReceiver(this); 

             int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
             int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
             int level = -1;  
             if (rawlevel >= 0 && scale > 0) {  
               level = (rawlevel * 100) / scale;  
             }  

             int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);  

             String strStatus = "";  
             switch (status) {
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                     strStatus= "Unknown Charged";
                    break;
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    strStatus = "Charged Plugged";
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    strStatus = "Charged Unplugged";
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    strStatus = "Not Charging";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    strStatus = "Charged Completed";
                    break;
             }
         }
    };


        HashMap<String, String> map = new HashMap<String, String>();
        map.put("col_1", DateFormat(cal.getTime()));
        int level = -1;          
        map.put("col_3", Integer.toString(level) + "%");
        String strStatus = "";
        map.put("col_4", strStatus);
        fillMaps.add(map);

    // fill in the grid_item layout
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
    lv.setAdapter(adapter);
}

    private String DateFormat(Date time) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd            HH:mm");
    //get current date time with Calendar()
    Calendar cal = Calendar.getInstance();
    return dateFormat.format(cal.getTime());    
    } 


    }

1 个答案:

答案 0 :(得分:3)

 private int getBatteryLevel(Context context) {
    int batteryPercentage = 0;
    try {
        IntentFilter ifilter = new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,
                -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE,
                -1);

        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,
                -1);

        float batteryPct = level / (float) scale;

        batteryPercentage = (int) (batteryPct * 100);

        if (batteryPercentage < 0) {
            batteryPercentage = 0;

        }

        Log.d(TAG, "Battery level remaining  " + batteryPercentage);

        String strStatus = "";
        switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            strStatus = "Unknown Charged";
            break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
            strStatus = "Charged Plugged";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            strStatus = "Charged Unplugged";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            strStatus = "Not Charging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            strStatus = "Charged Completed";
            break;
        }

        Log.d(TAG, "battery status  " + strStatus);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return batteryPercentage;
}