public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = "Hellooo, alrm worked ----";
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(context, TripNotification.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
public void setAlarm(Context context){
Log.d("Carbon","Alrm SET !!");
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 30 seconds to the calendar object
cal.add(Calendar.SECOND, 30);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
}
This is the final code i managed to get working. You need to add
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
just above the </application>
tag in Manifest file.
This will set an alarm to trigger in 30 seconds after calling the method SetAlarm()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…