Reputation: 416
Intent i = new Intent(getBaseContext(), MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 100,i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, pi);
}
});
}
private void create()
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String tag="mm";
Log.d(tag,"xyz");
NotificationChannel notificationChannel = new NotificationChannel("abc","abc",NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
//Receiver class
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder notification = new NotificationCompat.Builder(context,"abc")
.setContentTitle("Title")
.setContentText("text")
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setSmallIcon(R.drawable.ic_launcher_background);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1,notification.build());
}
}
Manifest file is updated also. I've called create function in onCreate. If anyone can help me. I've been stuck on this for a week
Upvotes: 2
Views: 2571
Reputation: 10549
One of the first considerations in using an alarm is what its type should be -
There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.
ELAPSED_REALTIME
—Fires the pending intent based on the amount of time since the device was booted, but doesn't wake up the device. The elapsed time includes any time during which the device was asleep.
ELAPSED_REALTIME_WAKEUP
—Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.
Also, SystemClock#elapsedRealtime()
returns milliseconds since boot, including time spent in sleep. Try replacing that with System#currentTimeMillis()
that returns the current time in milliseconds.
For Waking up the device to fire the pending intent at the specified time. You could use RTC_WAKEUP
alarm type.
AFAIK, there is no behavior change in AndroidQ that have an impact on your context.
You may also have a look at this S/O thread
Upvotes: 1