电池是因为alarmManager和广播接收机的排放,我认为 [英] Battery is draining because of alarmManager and Broadcast Receiver I think

查看:208
本文介绍了电池是因为alarmManager和广播接收机的排放,我认为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个像的AppLock一个应用程序(没有的AppLock),检查当前的应用程序由用户与选定的应用程序列表运行,如果再符合我HV我的code。我有可是没有任何一个循环服务电话Startu preceiver。类(广播接收器),它依次调用CheckRunningApplicationReceiver.Class(广播接收器),它会检查当前活动。我打电话CheckActivity为每0.5秒。我也很多存储,并在里面CheckActivity.Class内部存储检索的字符串。它耗尽我的电池。帮帮我吧。

MyService.class

 公共类的MyService扩展服务{
私有静态最后弦乐TAG =为MyService@覆盖
公众的IBinder onBind(意向为arg0){
    // TODO自动生成方法存根
    返回null;
}
@覆盖
公共无效的onCreate(){}@覆盖
公共无效调用onStart(意向意图,诠释startId){//注意:您可以开始一个新的线程,并用它长期后台处理从这里开始。
}@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    // TODO自动生成方法存根
        getBaseContext()。getApplicationContext()。sendBroadcast(
                新的意图(Startu preceiver_Manual_Start));    返回START_STICKY;}}

Startu preceiver.Class

 公共类Startu preceiver延伸广播接收器{
静态最后弦乐TAG =SR;
最终诠释startupID = 1111111;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    最后AlarmManager alarmManager =(AlarmManager)上下文
            .getSystemService(Context.ALARM_SERVICE);    尝试{            意图I7 =新意图(背景下,CheckRunningApplicationReceiver.class);
               的PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(背景下,
                    startupID,i7处理器,0);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime(),
                    500,ServiceManagementIntent);
        }赶上(例外五){
            Log.i(TAG,异常:+ E);
        }}
      }
    }

}

CheckRunningApplicationReceiver.Class

 公共类CheckRunningApplicationReceiver扩展广播接收器实现Serializable {
@覆盖
公共无效的onReceive(上下文aContext,意图anIntent){    //我在这里做很多事情。
   //我没有使用任何线程在这里(我不知道关于线程)。
   //我在这里检查互联网连接,存储的检索
   // ArrayList中,从这里内部存储字符串,获取当前
   //运行应用程序和ArrayList的检查。
}


  • 下面是我观察到,当我把报警时间为10毫秒的事情,当连接到笔记本电脑我的电话,电话的手机检测后延时15秒发生的,通过蓝牙传输文件,即使,有一个延迟。现在我打电话0.2秒。因此,我不能看到这些问题。请告诉我这样做的原因是什么?

  • 我存储ArrayList和几串在内部存储。我应该使用共享preferences呢?

  • 有没有广播接收器,它告诉我,如果当前运行的应用程序被改变?

  • 我应该使用一个服务里面一个无限循环,而不是使用报警管理器?

  • 如何在手机锁定或手机进入睡眠状态?停止CHeckRunningApp.Class

  • 解决方案电池漏电?


解决方案

  

但我需要当用户启动一个应用程序来检查


有没有办法检测到新的应用程序启动(我不认为有任何这样的广播接收器来支持),你可以写这将继续轮询来寻找利用活动管理器运行应用程序的服务,但我不要以为这将是有效的。你必须妥协,eith轮询速度缓慢或电池可能会耗尽快。

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

HI i have an app like a applock (not applock) , which checks for current app running with list of selected apps by a user , if it matches then I hv my code . I have an service which doesnt have any loops calls StartupReceiver .Class(broadcast Receiver) which inturn calls CheckRunningApplicationReceiver.Class(broadcastReceiver) which checks the current activity . I am calling CheckActivity for every 0.5 second . And i do lot of storing and retrieving strings in internal storage inside the CheckActivity.Class . Its draining my battery . Help me .

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

  • Here are the things I observed , when I put alarm time as 10ms , my phone when connected to laptop , detection of phone phone happened after 15 seconds delay , even when transferring files through bluetooth ,there was a delay . now i am calling for 0.2 second . So I cannot observe these problems. Whats the reason for this ?
  • I am storing arraylist and few strings in the internal storage . Should I be using Shared Preferences instead?
  • Is there any broadcast receiver which tells me if current running app is changed ?
  • Should i be using a infinite loop inside a service instead of using alarm Manager?
  • How to stop the CHeckRunningApp.Class when the phone is locked or phone goes to sleep ?
  • Solution for battery drain?

解决方案

But I need to check when an user starts an app

There is no way to detect the new application launch (I don't think there's any such Broadcast receiver to support that), you can write a service which would keep on polling to look for running application using Activity Manager, but I don't think that would be efficient. You have to compromise, eith poll a a slow rate or Battery may drain fast.

Please have a look at this project - https://github.com/twinone/AppLocker

这篇关于电池是因为alarmManager和广播接收机的排放,我认为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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