我制作了一个非常简单的应用程序并在通知上放了一个图标。
现在,我想在通知中放置一个停止按钮,所以我开始使用.addaction
但是当我尝试传递这个带有额外数据的pIntentStop时,我无法在“on new intent”中恢复它
我正在使用singleInstance。
此文件是我的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);
}
答案 0 :(得分:0)
解决了,我把公共静态最终字符串ACTION_STOP =" STOP&#34 ;;作为全球和其他人:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onPause() {
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.setAction(MainActivity.ACTION_STOP);
PendingIntent pIntentStop = PendingIntent.getActivity(this, 0,
intentStop, 0);
Notification 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;
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
noti.flags |= Notification.PRIORITY_MAX;
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
super.onPause();
}
public void openStop(View v) {
openStop();
}
public void openStop() {
myWebView.loadUrl("about:blank");
myWebView.clearCache(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
System.exit(0);
}
@Override
protected void onNewIntent(Intent intent) {
if (intent.getAction() != null) {
String action = intent.getAction();
if (action.equals(MainActivity.ACTION_STOP)) {
this.finish();
openStop();
}
}
super.onNewIntent(intent);
}