这个问题基于我之前的问题:Android - 将通知设置为永不消失
我现在意识到我需要在启动时触发一些东西来触发通知。
从我之前的问题来看,我可以采取两条路线。要么启动服务,要么使用广播接收器。我认为启动服务有点矫枉过正(但我知道什么)。我在使用 BroadcastReceiver 路由时被卡住了。它通常可以工作,但是当我将代码用于通知时,我会收到一堆错误。
这是我的代码:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
//Intent.putExtra("My Notification");
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(i, mBuilder.build());
我的错误:
对此有什么想法吗?
摘要:服务或广播接收器更好(在这种情况下)?如何解决我的代码中的这些错误(当您将鼠标悬停时,他们说它们未定义)?