我有一个像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 .
}
答案 0 :(得分:-1)
是的,我明白了。每半秒,如果您检查互联网连接,打开或关闭它,检查当前的运行活动,电池没有耗尽。 如果要在内部存储中存储或检索电池,电池会耗尽。 现在我正在使用共享首选项,我没有电池耗尽。呀!