【问题标题】:getIntent.remove extra not workinggetIntent.remove extra 不起作用
【发布时间】:2019-08-07 19:53:52
【问题描述】:
public void showNotification(Context context,String pnrNumber){

        Intent intent=new Intent(context,HomeActivity.class);
        intent.putExtra("PNR", pnrNumber);

        //To Clear the Activity Stack
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNumber,intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("TravelKhana")
                .setContentText("Get food in train for the PNR:" +pnrNumber);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(uniqueNumber, mBuilder.build());
        uniqueNumber++;

    }

在 HomeActivity 的 oncreate 中,我得到了额外的这个字符串

if(getIntent().hasExtra("PNR")){
                mPnrSearch.setTag(getIntent().getStringExtra("PNR"));
                onClick(mPnrSearch);
            }

然后在onClick(mPnrSearch);

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.pnrSearch:
            if(NetworkChecker.isConnected(getApplicationContext())) {
                easyTracker.send(MapBuilder.createEvent("Home Activity","click", "PNR", null).build());
            }
            Intent pnrIntent = new Intent(HomeActivity.this, PnrSearch.class);

            //If the user came from notification
            if(v.getTag() != null){
                pnrIntent.putExtra("PNR", v.getTag().toString());
                v.setTag(null);
                getIntent().removeExtra("PNR");
            }

            startActivity(pnrIntent);
            break;
}

我删除了额外的,然后我按下后退按钮以销毁应用程序并通过长按手机中的主页按钮重新打开它,然后在额外的仍然存在并且再次调用 onClick(mPnrSearch) 之后,但我有删除了多余的为什么会这样??以及我需要做些什么来解决这个问题。

【问题讨论】:

    标签: java android android-intent android-notifications


    【解决方案1】:

    这要么是 Android 错误,要么是一项功能,取决于您是否希望它发生 ;-) 这种情况没有明确记录,但很明显它的行为方式与您描述的一样。

    我最近回答了一个类似的问题,并就如何处理这个问题提出了一些建议。请参阅my answer 了解更多信息。

    【讨论】:

    • 谢谢,确实是这样,我已经解决了这个问题。
    【解决方案2】:

    我认为删除后你应该更新新的意图,只需在你的活动中实现这个功能

     protected void onNewIntent(Intent intent) {
            // TODO Auto-generated method stub
            super.onNewIntent(intent);
            setIntent(intent);
        }
    

    【讨论】:

      【解决方案3】:

      在我的例子中,我将通知中的值从 onMessageReceived 传递给了我的 MainActivity。为什么因为我想通过通知点击来计算打开的应用程序以获取特定的通知 id,这是我出于分析原因通过 exteras(通知打开应用程序 mainactivity)传递的值。无论如何,要确定主要活动是否通过通知打开,我检查了 exteras 值和意图标志。我观察到,当通过通知打开应用程序时,单击意图标志为 0,因此我将以下内容添加到我的 mainactivity。

      if(getIntent().getFlags() == 0 && getIntent().getExtras() != null){
                  Log.i("opened by notification only",getIntent().getStringExtra("myID"));
                  //rest of process...............
              }
      

      【讨论】:

        猜你喜欢
        • 2021-08-28
        • 1970-01-01
        • 2021-09-22
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 2014-12-04
        • 1970-01-01
        • 2020-05-02
        相关资源
        最近更新 更多