android-如何为充电日志检测电池电量和状态?

android-如何为充电日志检测电池电量和状态?,第1张

概述我正在尝试为android中的电池进行充电记录,并且工作正常.但是问题是我无法检测到电池电量以及充电器的状态.供您参考,我是使用android的初学者,希望有人可以帮助我编写代码.publicclassListViewAextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@Ove

我正在尝试为android中的电池进行充电记录,并且工作正常.但是问题是我无法检测到电池电量以及充电器的状态.供您参考,我是使用androID的初学者,希望有人可以帮助我编写代码.

    public class ListVIEwA extends Activity {/**Called when the activity is first created.*/@OverrIDeprotected 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());        }     }

解决方法:

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

总结

以上是内存溢出为你收集整理的android-如何为充电日志检测电池电量和状态?全部内容,希望文章能够帮你解决android-如何为充电日志检测电池电量和状态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077081.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存