由于alarmManager和广播接收器我认为电池正在耗尽

时间:2015-04-13 11:58:51

标签: android service broadcastreceiver alarmmanager battery

我有一个像applock(不是applock)这样的应用程序,它会检查用户运行的当前应用程序与所选应用程序的列表,如果它匹配,那么我的代码。我有一个服务,它没有任何循环调用StartupReceiver .Class(广播接收器),它调用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和少量字符串。我应该使用共享首选项吗?
  • 是否有任何广播接收器告诉我当前正在运行的应用程序是否已更改?
  • 我应该在服务中使用无限循环而不是使用警报管理器吗?
  • 如何在手机锁定或手机进入睡眠状态时停止CHeckRunningApp.Class?
  • 电池耗尽的解决方案?

1 个答案:

答案 0 :(得分:-1)

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