[英]AlarmManager stops working after a few days
I have a service which should be run for example every 1 minute. 我有一项服务,例如应该每1分钟运行一次。 I used a broadcast receiver and AlarmManager to make this work.
我使用了广播接收器和AlarmManager来完成这项工作。 And also I call PowerManger.aquire() to make sure cpu doesn't sleep before the service starts.
而且我还调用PowerManger.aquire()来确保cpu在服务启动之前不会进入睡眠状态。 For first 2 or 3 days the app runs okay but after that the service doesn't get started.
在最初的2或3天内,该应用可以正常运行,但此后该服务无法启动。 Sounds alarmManager doesn't start it.
听起来AlarmManager无法启动。 Any Idea why?
知道为什么吗?
public class MyReceiver extends BroadcastReceiver {
PowerManager pawerManager;
public static PowerManager.WakeLock wakeLock=null;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
pawerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pawerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
Intent serviceIntent=new Intent(context,MyService.class);
context.startService(serviceIntent);
}
}
And The Service: 和服务:
public class MyService extends Service {
void releaseTheLock(){
if (MyReceiver.wakeLock != null){
MyReceiver.wakeLock.release();
MyReceiver.wakeLock=null;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
final Context serviceContext=this;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
/*
Do something
*/
//Now set the timer
long currntTime = System.currentTimeMillis();
AlarmManager mgr=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent i= new Intent(serviceContext, MyReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(serviceContext, 0, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, currntTime + 60000 , pi);
stopSelf();
releaseTheLock();
return;
}
}).start();
return START_STICKY;
}
}
And here's the receiver registration in manifest: 这是清单中的接收者注册:
<receiver android:name=".TimeReceiver"></receiver>
I suspect you're running into a race condition where the Service
object ( Context
) is being cleaned up and destroyed, but is being used as the Context
for your PendingIntent
. 我怀疑您正在进入一种竞争状态,其中
Service
对象( Context
)正在被清理和销毁,但是被用作PendingIntent
的Context
。 Here are a couple of options: 这里有几个选择:
Change your creation of PendingIntent
to use the application context. 将您创建的
PendingIntent
更改为使用应用程序上下文。 This context is the the one in which your PendingIntent
is sent. 此上下文是您发送
PendingIntent
上下文。 So if you use a transient context, like the Service
object itself, it may no longer be valid. 因此,如果您使用过渡上下文,例如
Service
对象本身,则它可能不再有效。
Revise this to not use the BroadcastReceiver
at all. 修改它根本不使用
BroadcastReceiver
。 You can create a PendingIntent
for your Service
via the PendingIntent.getService()
method. 您可以通过
PendingIntent.getService()
方法为您的Service
创建PendingIntent
。 Again, use your application context here rather than the Service
object itself. 同样,请在此处使用应用程序上下文,而不要使用
Service
对象本身。
Have you tried with setRepeating? 您是否尝试过setRepeating?
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60, alarmIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.