我发现了很多有关此问题的Stack Overflow帖子,但是它们都使用了现在不建议使用的Notification方法,而不是使用Notification.Builder类。
我已经成功创建了状态栏通知,但是单击时什么也没有发生。 LogCat中出现警告:
Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@412d6df0
这是我用来构建通知的代码:
public class LunchNotificationBuilder {
private Notification notification;
public LunchNotificationBuilder(Lunch lunch, Context context) {
Builder builder = new Builder(context);
Calendar reminderTime = (Calendar)lunch.getReminderTime().clone();
builder.setWhen(reminderTime.getTimeInMillis());
builder.setTicker("Reminder for " + lunch.getTitle());
builder.setContentTitle("LunchBunch Notification");
builder.setContentText("Upcoming lunch at " + lunch.getTitle());
builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent(context, InviteDetails.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT));
this.notification = builder.getNotification();
}
public Notification getNotification() {
return this.notification;
}
}
Lunch类只是我创建的数据结构,不必担心。
传入的上下文是Application.getApplicationContext()。
我正在按照文档中的建议设置Intent.FLAG_ACTIVITY_NEW_TASK标志,并且PendingIntent具有PendingIntent.FLAG_ONE_SHOT标志。
另一个注意事项:当我在代码中的其他位置显式调用startActivity时,我尝试启动的活动(InviteDetails)正常运行。关于此PendingIntent业务的某些信息无效。
最佳答案
原来我不知道如何使用Android。我单击的是顶部的状态栏,而不是向下拖动以查看完整的通知,然后再单击。我的代码正在工作。 h!