我正在尝试创建一个简单的函数来安排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 ++;
}
答案 0 :(得分:1)
我认为你忘了在android清单文件中输入新活动,所以只需输入下面给出的活动
<activity
android:name=".your activity name"
/>
并运行程序
答案 1 :(得分:0)
我找到了。问题是我没有将图标资源传递给通知构造函数。
notification = new Notification(R.drawable.icon, text, when);