我正在尝试制作一个在 Android 中安排本地通知的简单功能,但它不起作用。我错过了什么吗?即清单中的许可或类似的东西?我是android编程的新手。
static int notificationId = 0;
public void scheduleLocalNotification(String text, int seconds)
{
Context context = getApplicationContext();
long when = System.currentTimeMillis() + seconds * 1000;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyApp.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notification = new Notification();
notification.setLatestEventInfo(context, "My application", text, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.when = when;
notificationManager.notify(notificationId, notification);
notificationId ++;
}