每10分钟或改变Android的广播接收器的电池寿命,可以在其自己的线程去? [英] Android Broadcast Receiver battery life every 10 mins or change, can it go in its own thread?

查看:251
本文介绍了每10分钟或改变Android的广播接收器的电池寿命,可以在其自己的线程去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序(研究),使每一次电池电量变化的时间戳。如果我不能做到这一点,我想使它所以它需要一个电池阅读每10分钟左右。

我有这样的广播接收器 code,但我不知道在哪里把它。

我的应用程序保持与以下异常崩溃:


  

了java.lang.RuntimeException:错误接收广播意向{行动= android.intent.action.BATTERY_CHANGED FLG = 0x60000000(有临时演员)}在com.mdog.datareceive.Receive$1@43c9cd10


的onCreate 我的活动我产卵3 AsyncTask的线程在后台做的东西。哪里将是把广播接收器的好地方?

我曾尝试在的onCreate ,我已经在这所获取的后台任务一个叫新方法尝试。我认为这个问题可能是该函数的 BroadcastRecevier code为可能pmaturely结束$ P $?

反正是有,我可以把它放在自己的线程,以便它只是等待广播?

code:

  batteryLevelTimeStamps =新的LinkedList<串GT;();
广播接收器batteryLevelReceiver =新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        INT rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
        int标= intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
        INT级别= -1;
        如果(rawlevel> = 0&放大器;&安培;规模大于0){
            水平=(rawlevel * 100)/规模;
        }        batteryLevel =等级+%;
        batteryLevelTimeStamps.add(在时间:+新的Date()的toString()+电池电量是:+ batteryLevel);
        的out.print(在时间:+新的Date()的toString()+电池电量是:+ batteryLevel +中的onCreate \\ n);
    }
};IntentFilter的batteryLevelFilter =新的IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver,batteryLevelFilter);


解决方案

我想你想看看使用AlarmManager。您可以设置一个报警时,手机开机,并且您收到报警后,可以检查电池,并设定其他报警10分钟以后。

使用AlarmManager是比你自己的线程,因为

更好

  • 这让手机的十个分钟的等待
  • 中去低能耗模式
  • 在10分钟结束时,将唤醒手机,如果它是目前在低功耗状态。

I would like to write an application (for research) that makes a timestamp every time the battery level changes. If I can't do that, I want to make it so it takes a battery reading every 10 or so minutes.

I have this BroadcastReceiver code but I am not sure where to put it.

My application keeps crashing with the following exception:

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.mdog.datareceive.Receive$1@43c9cd10

In the onCreate of my activity I spawn 3 AsyncTask threads that do stuff in the background. Where would be a good place to put the broadcast receiver?

I have tried in the onCreate and I have tried in a new method that gets called by one of the background tasks. I think the problem might be that the function the BroadcastRecevier code is in might be ending prematurely?

Is there anyway I could put it in its own thread so that it just waits for broadcasts?

Code:

batteryLevelTimeStamps = new LinkedList<String>();
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent){
        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;
        }

        batteryLevel = level + "%";
        batteryLevelTimeStamps.add("At time: " + new Date().toString() + " the battery level is:" +batteryLevel); 
        out.print("At time: " + new Date().toString() + " the battery level is:" +batteryLevel + " in onCreate\n");
    }
};

IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);

解决方案

I think you want to look at using the AlarmManager. You can set an alarm when the phone boots up, and after you receive that alarm, you can check the battery and set another alarm 10 minutes in the future.

Using AlarmManager is better than your own thread because

  • It allows the phone to go to low power usage mode during the ten minute wait
  • At the end of 10 minutes, it will wake up the phone if it is currently in that low power state.

这篇关于每10分钟或改变Android的广播接收器的电池寿命,可以在其自己的线程去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆