Android 通知杀死应用程序,然后创建新实例

Android 通知杀死应用程序,然后创建新实例,android,android-notifications,Android,Android Notifications,我有两个按钮的通知。 当用户单击通知的任何按钮时,它应该: 1简历应用程序。 2响应已单击的通知按钮 现在应用程序响应点击的按钮,但每次应用程序被杀死,它都会创建一个新的实例,这是我不想要的。 我认为旗帜是错的。我尝试了与我的问题类似的其他堆栈解决方案中的标志,但我没有收到按钮的意图,所以我无法识别单击了哪个按钮 我如何在片段的onResume()中接收按钮意图: mNotificationManager = (NotificationManager) getActivi

我有两个按钮的通知。 当用户单击通知的任何按钮时,它应该:
1简历应用程序。
2响应已单击的通知按钮

现在应用程序响应点击的按钮,但每次应用程序被杀死,它都会创建一个新的实例,这是我不想要的。 我认为旗帜是错的。我尝试了与我的问题类似的其他堆栈解决方案中的标志,但我没有收到按钮的意图,所以我无法识别单击了哪个按钮

我如何在片段的onResume()中接收按钮意图:

mNotificationManager =
            (NotificationManager) getActivity().getSystemService(getActivity().getApplicationContext().NOTIFICATION_SERVICE);

    String extras = "";

    if (getActivity().getIntent().getExtras() != null) {
        Log.i(TAG, "[NOTIFICATION] Extras not null");
        extras = (String) getActivity().getIntent().getExtras().get("DO");
    }

    if (extras != null && extras != "") {
        Log.i(TAG, "[NOTIFICATION] Received notification bundle.");

        String btnClicked = extras;

        Log.i(TAG, "[NOTIFICATION BUTTON]" + btnClicked);

        if (btnClicked.equals(SMS_PAYMENT)) {
            Log.i(TAG, "[NOTIFICATION] SMS payment button cliked");
            createSmsPaymentDialog();
        }
        if (btnClicked.equals(SET_TIMER)) {
            Log.i(TAG, "[NOTIFICATION] Set timer button cliked");
            showTimer();
        }

        mNotificationManager.cancel(NOTIFICATION_ID);
如何创建通知:

  private void generateNotification() {

    Log.i(TAG, "Notification should arrive now.");

    RemoteViews contentView = new RemoteViews(getActivity().getPackageName(), R.layout.notification_layout);

    Notification noti = new Notification.Builder(getActivity().getApplicationContext())
            .setSmallIcon(R.drawable.scp_180)
            .build();

    noti.bigContentView = setNotificationListenersAndContent(contentView);

    noti.flags =  Notification.FLAG_AUTO_CANCEL;

    noti.defaults |= Notification.DEFAULT_LIGHTS; // LED
    //noti.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
   // noti.defaults |= Notification.DEFAULT_SOUND; // Sound

    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(NOTIFICATION_ID, noti);
}

public RemoteViews setNotificationListenersAndContent(RemoteViews view) {

    //setting Buttons
    Intent iSendSMS = new Intent(getActivity(), MainActivity.class);
    iSendSMS.putExtra("DO", "btnSendSms");
    iSendSMS.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    iSendSMS.setAction("btnSendSms");
    PendingIntent pApp = PendingIntent.getActivity(getActivity(), 0, iSendSMS, PendingIntent.FLAG_UPDATE_CURRENT); // byl ONE_SHOT
    view.setOnClickPendingIntent(R.id.btnSendSms, pApp);

    Intent iSetTimer = new Intent(getActivity(), MainActivity.class);
    iSetTimer.putExtra("DO", "btnSetTimer");
    iSetTimer.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    iSetTimer.setAction("btnSetTimer");
    PendingIntent pApp2 = PendingIntent.getActivity(getActivity(), 0, iSetTimer,PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnSetTimer, pApp2);

    //setting image
    view.setImageViewResource(R.id.notificationImage, R.drawable.scp_180);
    view.setTextViewText(R.id.notificationTitle, "some title");
    view.setTextViewText(R.id.notificationDescriptionText, "some content");

    return view;
}
编辑、添加logcat日志:

I/ScpFragment﹕ Notification should arrive now.  
I/ScpFragment﹕ OnPause
I/ScpFragment﹕ App in background: true
I/MainActivity﹕ onPause
I/MainActivity﹕ isFinishing = true, cleaning app.
I/MainActivity﹕ OnCreate..
I/MainActivity﹕ connecting to googleApi....
I/MainActivity﹕ onStart
I/MainActivity﹕ onResume
I/MainActivity﹕ GoogleApiClient connected
I/ScpFragment﹕ OnCreate..
I/ScpFragment﹕ OnCreateView
I/ScpFragment﹕ Setting Controlls and Listeners.
I/ScpFragment﹕ onResume..
I/ScpFragment﹕ App in background: false
I/ScpFragment﹕ [NOTIFICATION] Extras not null.
I/ScpFragment﹕ [NOTIFICATION] Reveived notification bundle.
I/ScpFragment﹕ [NOTIFICATION BUTTON]btnSetTimer
I/ScpFragment﹕ [NOTIFICATION] Set timer button cliked

请发布日志猫错误。@SiddharthVyas添加了日志。