Android AlarmManager stops when activity die

前端 未结 9 2121
鱼传尺愫
鱼传尺愫 2020-12-29 07:00

I start AlarmManager with PendingIntent and on few phones Alarm is not responding. On some devices is working ok on others it fails. I have made a few tests on different pho

9条回答
  •  独厮守ぢ
    2020-12-29 07:18

    i have also used Alarm Service in my project for preparative task in very 6 or 7 minutes. And it is running fine in all phone.

    i have make a alarm Service like this:

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.SystemClock;
    import android.util.Log;
    
    public class MyAlarmService {
        private static PendingIntent resetAlarm;
        private static String TAG="CellPoliceChildGPSAlarmService";
        private static AlarmManager am;
        public static void start(Context context) {
            try {
                // We want the alarm to go off 30 seconds from now.
                long firstTime = SystemClock.elapsedRealtime();
    
                // Create an IntentSender that will launch our service, to     be scheduled with the alarm manager.
                //resetAlarm = PendingIntent.getService(context, 0, new     Intent(context, Get_NonRootDetails.class), 0);
                resetAlarm = PendingIntent.getService(context, 0, new     Intent(context, CallNonRBackgroundService.class), 0);
                // Schedule the alarm!
                am = (AlarmManager)     context.getSystemService(Context.ALARM_SERVICE);
                Log.i(TAG, firstTime+"");
                am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,     firstTime, 10*1000*60, resetAlarm);
            } 
            catch (Exception e) {
                Log.v("CellInfo", "Exception while start the     MyAlarmService at: " + e.getMessage());
            } 
        }
    
        public static void stop(Context context) {
            try {
                // When interval going to change from web services
                am.cancel(resetAlarm);
            } 
            catch (Exception e) {
                Log.v("CellInfo", "Exception while start the     MyAlarmService at: " + e.getMessage());
            }
        }
    
    
    }
    

    I have called or start like this;

    MyAlarmService.start(SplashActivity.this);
    

    Given permission in Manifest:

    
    
        
                
                    
                
            
    

    For notifications i also used pending intents like;

        Intent notificationIntent = new Intent(context, DashBoardActivity.class);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(context,     0, notificationIntent, 0);
    
            notification.setLatestEventInfo(context, contentTitle,     PushNotificationUtils.notiMsg, pendingIntent);
            notification.flags  |=  notification.FLAG_AUTO_CANCEL;
    

提交回复
热议问题