0

我正在尝试制作一个在 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 ++;

}
4

2 回答 2

1

我认为您忘记在 android 清单文件中输入新活动,所以只需输入下面给出的活动

 <activity
        android:name=".your activity name"
         />

并运行程序

于 2012-12-14T19:00:05.020 回答
0

我找到了。问题是我没有将图标资源传递给通知构造函数。

notification = new Notification(R.drawable.icon, text, when);
于 2012-12-17T17:39:32.063 回答