Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple AlarmManager example for firing an activity in 10 minutes

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 fire an activity in 10 (let's say) minutes, that's it. Thank you.

like image 583
good_evening Avatar asked Oct 08 '22 03:10

good_evening


1 Answers

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>
like image 60
vinothp Avatar answered Oct 13 '22 12:10

vinothp