Setting up Alarm Manager is creating 2 Instances of my Main Activity

前端 未结 3 2082
闹比i
闹比i 2020-12-06 23:51

I have 2 activities, a Main Activity and SetAlarm Activity. I call SetAlarm Activity from Main. When I set up the alarm I create an instance of my main. How do I set up the

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 00:10

    I dont know why you have the SetAlarm Activity, you dont need a activity to set the alarm. Anyways, AlarmManager is a pain to get working. It took me a while to get it up and running. This is what I have in my code now, running.

            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 5);
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
            notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            notifyintent.setAction("android.intent.action.NOTIFY");
            PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
                    notifysender);
    

    OnAlarmReceiver

    public class OnAlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // PullPendingRequests.acquireStaticLock(context)
            try {
                lock = getLock(context);
                lock.acquire();
                context.startService(new Intent(context, UpdateCustomerRequests.class));
            } finally {
                if (lock.isHeld()) {
                    lock.release();
                }
            }
        }
    
        private static final String NAME = "com.commonsware.cwac.wakeful.WakefulIntentService";
        private static volatile PowerManager.WakeLock lockStatic = null;
        private static PowerManager.WakeLock lock;
    
        synchronized private static PowerManager.WakeLock getLock(Context context) {
            if (lockStatic == null) {
                PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    
                lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
                lockStatic.setReferenceCounted(true);
            }
    
            return (lockStatic);
        }
    }
    

    IntentService which is called by OnAlarmReceiver

    public class UpdateCustomerRequests extends IntentService {
    @Override
        final protected void onHandleIntent(Intent intent) {
            //
            //Your stuff here
            //
        }
    
        public class LocalBinder extends Binder {
            public UpdateCustomerRequests getService() {
                return UpdateCustomerRequests.this;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return bindToHomeScreen;
        }
    }
    

    Android Manifest

    • Inside manifest tag

    • Inside application tag

          
              
                  
              
          
      

提交回复
热议问题