【问题标题】:Battery is draining because of alarmManager and Broadcast Receiver I think由于 alarmManager 和 Broadcast Receiver 我认为电池正在耗尽
【发布时间】:2015-04-13 11:58:50
【问题描述】:

嗨,我有一个像 applock(不是 applock)这样的应用程序,它检查当前应用程序运行的用户选择的应用程序列表,如果它匹配,那么我会使用我的代码。我有一个没有任何循环的服务调用 StartupReceiver .Class(broadcast Receiver) 反过来调用 CheckRunningApplicationReceiver.Class(broadcastReceiver) 来检查当前活动。我每 0.5 秒调用一次 CheckActivity 。我在 CheckActivity.Class 的内部存储中做了很多存储和检索字符串。它耗尽了我的电池。帮帮我。

MyService.class

public class MyService extends Service{
private static final String TAG = "MyService";

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public void onCreate() {      

}

@Override
public void onStart(Intent intent, int startId) {

//Note: You can start a new thread and use it for long background processing from here.
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub      
        getBaseContext().getApplicationContext().sendBroadcast(
                new Intent("StartupReceiver_Manual_Start")); 

    return START_STICKY;

}}

StartupReceiver.Class

public class StartupReceiver extends BroadcastReceiver {
static final String TAG = "SR";
final int startupID = 1111111;


@Override
public void onReceive(Context context, Intent intent) {
    final AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    try{

            Intent i7 = new Intent(context, CheckRunningApplicationReceiver.class);                        
               PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(context,
                    startupID, i7, 0);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime(), 
                    500, ServiceManagementIntent);


        } catch (Exception e) {
            Log.i(TAG, "Exception : "+e);
        }}
      }  
    }

}

CheckRunningApplicationReceiver.Class

 public class CheckRunningApplicationReceiver extends BroadcastReceiver implements Serializable{




@Override
public void onReceive(Context aContext, Intent anIntent) {

    //I do lot of things here . 
   //I am not using any thread here ( i dont know about threads ) .
   //I am checking internet connectivity here , storing an retrieving
   //arraylist , strings from internal storage here , getting current 
   //running app and checking with the arraylist . 
}
  • 这是我观察到的事情,当我把闹钟时间设置为10ms时,我的手机连接到笔记本电脑时,在15秒延迟后检测到手机电话,即使通过蓝牙传输文件也有延迟。现在我要求 0.2 秒。所以我无法观察到这些问题。这是什么原因?
  • 我在内部存储中存储了 arraylist 和几个字符串。我应该改用共享首选项吗?
  • 是否有任何广播接收器可以告诉我当前运行的应用程序是否已更改?
  • 我应该在服务中使用无限循环而不是使用警报管理器吗?
  • 手机锁定或休眠时如何停止CCheckRunningApp.Class?
  • 电池耗尽的解决方案?

【问题讨论】:

  • 如果您继续以 0.2 秒进行轮询,电池肯定会很快耗尽。
  • 但是我需要检查用户何时启动应用程序,如果应用程序与列表匹配。那么我如何在不短时间调用它的情况下做到这一点呢?有什么不同的方法吗?

标签: android service broadcastreceiver alarmmanager battery


【解决方案1】:

但我需要检查用户何时启动应用

没有办法检测到新应用程序的启动(我认为没有任何此类广播接收器支持),您可以编写一个服务,它会继续轮询以使用 Activity Manager 查找正在运行的应用程序,但我不要认为那将是有效的。您必须做出妥协,要么降低轮询速度,要么电池消耗很快。

请看看这个项目 - https://github.com/twinone/AppLocker

【讨论】:

  • 如果我只使用服务而不是警报管理器和广播接收器,会有所不同吗?知道 applock 之类的应用是如何做到的吗?
  • 我又做了一个观察。如果我删除 CheckRunningApps.class 中存在的代码,即虽然该类被调用 0.5 秒,但我的电池没有耗尽。所以我需要划分我的工作并在不同的接收器和多个线程中运行它们我猜。谁能告诉我有关多线程的信息?
  • 所以我有一个 arraylist 。我正在使用 Gson.jar 将其转换为 json 字符串并使用共享首选项存储它。现在我的电池正在耗尽,其百分比与手机理想状态百分比相同。无论如何我可以改进它吗?
  • 问题是你每半秒做一次,如果你继续这样做肯定会消耗电池。
【解决方案2】:

是的,我明白了。每半秒钟,如果您检查互联网连接,打开或关闭它,检查当前的运行活动,电池不会耗尽。 如果您在内部存储器中存储或检索,电池会耗尽。 现在我正在使用共享首选项并且我没有电池耗尽。对!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多