【问题标题】:How to get intent.getExtras from notification.addaction to work?如何从 notification.addaction 获得 intent.getExtras 工作?
【发布时间】: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


【解决方案1】:

解决了,我把 public static final String ACTION_STOP = "STOP";作为全球和其他人:

@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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 2020-08-16
    • 2018-10-17
    • 2011-04-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多