【发布时间】:2014-08-14 12:11:22
【问题描述】:
我做了一个非常简单的应用程序,并在通知上放了一个图标。
现在,我想在通知中放置一个停止按钮,所以我开始使用 .addaction
但是当我尝试传递这个带有额外数据的 pIntentStop 时,我无法在“新意图”中恢复它
我正在使用单实例。
这个文件是我的 MainActivity:
@Override
protected void onPause() {
Notification noti;
Intent intent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent intentStop = new Intent(MainActivity.this, MainActivity.class);
intentStop.putExtra("method", "openStop");
PendingIntent pIntentStop = PendingIntent.getActivity(this, 0,
intentStop, 0);
noti = new NotificationCompat.Builder(this)
.setContentTitle(this.getString(R.string.app_name))
.setContentText(this.getString(R.string.resume))
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.shutdown, this.getString(R.string.stop),
pIntentStop).setContentIntent(pIntent).build();
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_NO_CLEAR;
noti.flags |= Notification.FLAG_ONGOING_EVENT;
noti.flags |= Notification.PRIORITY_MAX;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
super.onPause();
}
public void openStop(View v) {
myWebView.loadUrl("about:blank");
myWebView.clearCache(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
System.exit(0);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Intent sender = getIntent();
String extraData = sender.getExtras().getString("method");
if (intent.getExtras() != null && extraData.equals("openStop")) {
Log.d(TAG, "method: " + extraData);
System.exit(0);
}
【问题讨论】:
-
你为什么试图启动一个活动来删除一个通知只是为了立即调用完成并重新添加通知?
-
我会使用服务来取消通知。
-
我只想让 openStop 被 .addaction 意图调用。
标签: android android-intent android-activity