简体   繁体   English

几天后AlarmManager停止工作

[英]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 )正在被清理和销毁,但是被用作PendingIntentContext Here are a couple of options: 这里有几个选择:

  1. 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对象本身,则它可能不再有效。

  2. 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.

相关问题 带BOOT COMPLETE广播接收器的AlarmManager几天后停止工作 - AlarmManager with BOOT COMPLETE broadcast receiver stop working after few days 几天后,通过AlarmManager安排的Android服务将停止调用 - Android Service scheduled with AlarmManager stops being called after a couple of days 如何更新OREO中的Widget(AlarmManager在几小时/几天后停止) - How to update a Widget in OREO (AlarmManager stops after some hours/days) 广播接收器几天后停止接收(本地通知) - BroadcastReceiver stops receiving after few days (Local Notification) 几个小时后,TextToSpeech停止工作 - TextToSpeech stops working after a few hours Android程序在几次使用后停止工作 - Android program stops working after a few uses AlarmManager和IntentService最终停止工作 - AlarmManager and IntentService stops working eventually 播放音乐的Android应用几秒钟后停止工作 - Android app playing music stops working after a few seconds 手机锁定数小时后,BroadcastReceiver停止工作 - BroadcastReceiver stops working after few hours the phone was locked AlarmManager在Android 4.4.2中停止工作(使用SetExact()) - AlarmManager stops working in Android 4.4.2 (using SetExact())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM