Battery is draining because of alarmManager and Broadcast Receiver I think

流过昼夜 提交于 2019-12-12 02:22:09

问题


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?

回答1:


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




回答2:


Yes I got it . For every half a second , if you check internet connection , turn it on or off , check current running activity , battery doesnt get drained . Battery gets drained if you are storing or retrieving in the internal storage . Now i am using shared preferences and I am not having an battery drain . Yeah!



来源:https://stackoverflow.com/questions/29604768/battery-is-draining-because-of-alarmmanager-and-broadcast-receiver-i-think

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!