Simple AlarmManager example for firing an activity in 10 minutes

后端 未结 2 2031
感动是毒
感动是毒 2021-01-01 04:35

I\'ve found many similar questions to this, but they\'re too complicated (too much code), at least I think.

Can this thing be done in a few code of lines? I want to

相关标签:
2条回答
  • 2021-01-01 05:23

    This function I use sets or cancels an alarm depending on the "Set" parameter

    public static void SetAlarm(Context c, long AlarmTime, int ItemID, String Message, Boolean Set) {
        Intent intent = new Intent(c, AlarmReceiver.class);
        intent.putExtra("Message", Message);
        intent.putExtra("ItemID", ItemID);
    
        PendingIntent sender = PendingIntent.getBroadcast(c, 8192 + ItemID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
                // Clear the seconds to 0 for neatness
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(AlarmTime);
        ca.set(Calendar.SECOND, 0);
        AlarmTime = ca.getTimeInMillis();
    
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
        if (Set) {
            am.set(AlarmManager.RTC_WAKEUP, AlarmTime, sender);
        } else {
            am.cancel(sender);
        }
    }
    

    You would then need a Broadcast Receiver to handle the alarm and do whatever it is you want to do.

    public class AlarmReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String Message = bundle.getString("Message");
            int ItemID = bundle.getInt("ItemID");
    
            // Do what you want to do, start an activity etc
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-01 05:24

    To Set Alarm for 10 Minutes(let's say) Use this code

     AlarmManager alarmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
     Intent intent = new Intent(this, ShortTimeEntryReceiver.class);   
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),10*60*1000, pendingIntent); 
    

    To Start Activity

    public class ShortTimeEntryReceiver extends BroadcastReceiver{
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
     try {
             Bundle bundle = intent.getExtras();
             String message = bundle.getString("alarm_message");
    
             // Your activity name
             Intent newIntent = new Intent(context, ReminderPopupMessage.class); 
             newIntent.putExtra("alarm_message", message);
             newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
             context.startActivity(newIntent);
            } catch (Exception e) { 
             e.printStackTrace();
    
            } 
    } 
    }
    

    In your Manifest File Add the following

     <receiver android:name=".ShortTimeEntryReceiver"
                          android:enabled="true"
                          android:process=":remote"> 
                </receiver>
    
    0 讨论(0)
提交回复
热议问题