【发布时间】: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