簡體   English   中英

如何從notification.addaction獲取intent.getExtras以工作?

[英]How to get intent.getExtras from notification.addaction to work?

我做了一個非常簡單的應用程序,並在通知上放了一個圖標。

現在,我想在通知中放置一個停止按鈕,因此我開始使用.addaction

但是,當我嘗試傳遞帶有額外數據的pIntentStop時,我無法在“基於新意圖”中恢復它

我正在使用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);
    }

解決了,我把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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM