如何在android中显示电源连接时长?

时间:2015-03-03 07:00:26

标签: java android

我正在尝试在我的应用中显示连接和断开电源之间的持续时间....我正在尝试像下面这样做...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.batterystat);
batteryStatInfo = (TextView) findViewById(R.id.batteryStatusInfo);
    this.registerReceiver(this.batteryStatRec, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    private BroadcastReceiver batteryStatRec = new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent) 
        {
           int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
            int icon = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
int chargingBy = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            boolean isChargingByUsb = chargingBy == BatteryManager.BATTERY_PLUGGED_USB;
            boolean isChargingByAc = chargingBy == BatteryManager.BATTERY_PLUGGED_AC;
if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED))
            {
                time = new Time();
                time.setToNow();
                shour = time.hour;
                sminutee = time.minute;

                prefs.edit().putInt("shour", shour).commit();
                prefs.edit().putInt("sminute", sminutee).commit();
                statusBat += "Power Connected \n";
            }

            else if(intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED))
            {
                time = new Time();
                time.setToNow();
                ehour = time.hour;
                eminutee = time.minute;

                prefs.edit().putInt("ehour", ehour).commit();
                prefs.edit().putInt("eminute", eminutee).commit();
                statusBat += "Power Disconnected \n";
            }

            int eHour = prefs.getInt("ehour", 0);
            int sHour = prefs.getInt("shour", 0);
            int eMinute = prefs.getInt("eminutee", 0);
            int sMinute = prefs.getInt("sminutee", 0);
            int fHour = eHour - sHour;
            int fMinute = eMinute - sMinute;

            //statusBat += "Total ChargingTime : "+fHour+":"+fMinute+"\n";

            batteryStatInfo.setText(statusBat);
            //batteryStatImage.setImageResource(icon);

        };
    };

但我没有得到结果....我的应用程序一直崩溃......有没有其他方法可以做到?

错误:

03-03 13:04:32.910: E/AndroidRuntime(28227): FATAL EXCEPTION: main
03-03 13:04:32.910: E/AndroidRuntime(28227): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.rhs.systmenetworkstat.BatteryStatus$1@419261e0
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.os.Handler.handleCallback(Handler.java:800)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.os.Handler.dispatchMessage(Handler.java:100)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.os.Looper.loop(Looper.java:194)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.app.ActivityThread.main(ActivityThread.java:5410)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at java.lang.reflect.Method.invoke(Method.java:525)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at dalvik.system.NativeStart.main(Native Method)
03-03 13:04:32.910: E/AndroidRuntime(28227): Caused by: java.lang.NullPointerException
03-03 13:04:32.910: E/AndroidRuntime(28227):    at com.rhs.systmenetworkstat.BatteryStatus$1.onReceive(BatteryStatus.java:139)
03-03 13:04:32.910: E/AndroidRuntime(28227):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
03-03 13:04:32.910: E/AndroidRuntime(28227):    ... 9 more

1 个答案:

答案 0 :(得分:0)

private final BroadcastReceiver receiver = new BroadcastReceiver() {
       @SuppressWarnings("deprecation")
    @Override
       public void onReceive(final Context context, Intent intent) {

          String action = intent.getAction();
          if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
                    // Do something when power connected
             //save time in proper format in preference or anywhere
                }
                else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                    // Do something when power disconnected
                    //take the time differene between connted time that
                    // was saved and current time and dispay it

                      tv.setText(time_differenece);
                }

          }

    };
IntentFilter filter = new IntentFilter();
  filter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
  filter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
  registerReceiver(receiver, filter);