12
A
回答
16
您需要使用PendingIntent
和BroadCastReceiver
此 -
public void scheduleNotification(Context context, long delay, int notificationId) {//delay is after how much time(in millis) from current time you want to schedule the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.title))
.setContentText(context.getString(R.string.content))
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.app_icon)).getBitmap())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent intent = new Intent(context, YourActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(activity);
Notification notification = builder.build();
Intent notificationIntent = new Intent(context, MyNotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
此外,你需要证明你的接收机类的通知 -
public class MyNotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(final Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(notificationId, notification);
}
}
最后,调用scheduleNotification()
用适当的参数和你很好去!
24
NOT FOR USE IN奥利奥+(编辑)
以上的回答都不错 - 但不考虑用户重启设备(它清除的PendingIntent的由AlarmManager计划)的潜力。
您需要创建一个WakefulBroadcastReceiver,它将包含一个AlarmManager来计划传递一个PendingIntent。当WakefulBroadcastReceiver处理该意图时 - 发布您的通知并发信号通知WakefulBroadcastReceiver完成。
WakefulBroadcastReceiver
/**
* When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent
* and then posts the notification.
*/
public class WakefulReceiver extends WakefulBroadcastReceiver {
// provides access to the system alarm services.
private AlarmManager mAlarmManager;
public void onReceive(Context context, Intent intent) {
//// TODO: post notification
WakefulReceiver.completeWakefulIntent(intent);
}
/**
* Sets the next alarm to run. When the alarm fires,
* the app broadcasts an Intent to this WakefulBroadcastReceiver.
* @param context the context of the app's Activity.
*/
public void setAlarm(Context context) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
//calendar.add(Calendar.SECOND, 10);
//ALWAYS recompute the calendar after using add, set, roll
Date date = calendar.getTime();
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);
// Enable {@code BootReceiver} to automatically restart when the
// device is rebooted.
//// TODO: you may need to reference the context by ApplicationActivity.class
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
/**
* Cancels the next alarm from running. Removes any intents set by this
* WakefulBroadcastReceiver.
* @param context the context of the app's Activity
*/
public void cancelAlarm(Context context) {
Log.d("WakefulAlarmReceiver", "{cancelAlarm}");
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
mAlarmManager.cancel(alarmIntent);
// Disable {@code BootReceiver} so that it doesn't automatically restart when the device is rebooted.
//// TODO: you may need to reference the context by ApplicationActivity.class
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
BootReceiver
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
context = ApplicationActivity.class;
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
//calendar.add(Calendar.SECOND, 10);
//ALWAYS recompute the calendar after using add, set, roll
Date date = calendar.getTime();
alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);
}
}
}
AndroidManifest.xml中
<receiver android:name=".WakefulReceiver"/>
<receiver android:name=".BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
相关问题
- 1. 安排未来的通知闹钟android
- 2. 在后台任务中安排通知
- 3. 如何在php中安排电子邮件通知
- 4. 如何在swift中安排相同的本地通知
- 5. Android安静通知
- 6. 无法安排通知
- 7. 如何安排在Android
- 8. 如何在android中安排短信?
- 9. 在显示android取消安排的通知之前
- 10. Android:在特定日期安排通知消息
- 11. 如何在指定时间安排通知给用户
- 12. 的Android听安装通知
- 13. 如何在android中显示通知?
- 14. 如何在Android中显示此通知?
- 15. 如何在android中获取BLE通知?
- 16. 如何安装Django通知?
- 17. 使用DatePicker安排通知(swift3)
- 18. 安排电子邮件通知
- 19. iOS - 安排多个本地通知,UNCalenderNotificationTrigger
- 20. 本地通知的日程安排号
- 21. 在android中安排事件
- 22. 在Android中安排事件
- 23. 在android中安排任务
- 24. 短信安排在Android中
- 25. 如何在Android上安排卡片?
- 26. 如何在iOS中安排可暂停或删除的本地通知?
- 27. 如何安排在iOS中只重复一次的本地通知
- 28. Swift:如何使用动态内容安排本地通知
- 29. 我们如何安排ios Firebase的推送通知
- 30. 如何安排重复的Apple推送通知?
这仍然是有用的贝科使用另一个问题是在此之前3年,一些潜在的行为已经改变,所以谢谢! – AfzalivE