Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通知getIntent().getExtras()获取相同的捆绑包_Android - Fatal编程技术网

Android 通知getIntent().getExtras()获取相同的捆绑包

Android 通知getIntent().getExtras()获取相同的捆绑包,android,Android,我遇到的问题是我调用sendAffimationNotice()两次,每次都传递一个唯一的aff_id(通过打印“put”+aff_id确认) 这将打印“放置1”,并在第二次调用“放置2” 手机现在有2个通知。尽管当两个都被单击时,它们在intents活动的onCreate方法中都有相同的ID 两次打印“ID为:1”,即使两个通知都是唯一的 public class PossAffNotification{ private static int notif_ID = 1; private st

我遇到的问题是我调用sendAffimationNotice()两次,每次都传递一个唯一的
aff_id
(通过打印“put”+
aff_id
确认)

这将打印
“放置1”
,并在第二次调用
“放置2”

手机现在有2个通知。尽管当两个都被单击时,它们在intents活动的onCreate方法中都有相同的ID

两次打印“ID为:1”,即使两个通知都是唯一的

public class PossAffNotification{

private static int notif_ID = 1;
private static NotificationManager notificationManager;
private static Notification note;
private static PendingIntent contentIntent;

public static void sendAffimationNotice(Context ctx, String title,String contentText,int aff_id){

    notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );

    note.defaults |= Notification.DEFAULT_SOUND;
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.defaults |= Notification.FLAG_AUTO_CANCEL;
    note.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent notificationIntent = new Intent(ctx, com.mindfsck.PossAff.MainActivity.class);
    notificationIntent.putExtra("aff_id",aff_id);
    System.out.println("putting " + aff_id);

    notificationIntent.setAction("com.mindfsck.PossAff.intent.action.aff");

    notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);

    note.setLatestEventInfo(ctx, title, contentText, contentIntent);

    notificationManager.notify(notif_ID,note);

    notif_ID += 1;
}

   };

    public class MainActivity extends Activity{
        @Override
        public void onCreate(Bundle savedInstanceState){

            super.onCreate(savedInstanceState);

            Intent serviceIntent = new Intent(this,PAService.class);
            this.startService(serviceIntent);

            Bundle extras = getIntent().getExtras();
            if(extras !=null) {
                Toast.makeText(getApplicationContext(), "ID is: " + extras.getInt("aff_id"),
                Toast.LENGTH_SHORT).show();
            }
        }
}

您的
意图
在所有非额外参数(如操作)上都是相同的。因此,您可以从
getActivity()
调用中获得相同的
pendingent
,因为每个不同的
Intent
只有一个
pendingent
。您需要在第二个
意图中更改一些内容,而不是附加内容,例如不同的动作字符串。

我的这篇文章可能会对您有所帮助:

这个问题基本上来自这样一个事实,即系统并不认为仅仅因为我们将一个新的意图对象传递给挂起的意图,我们就希望这个新的意图对象被传递,而只是让旧的(初始的)挂起的意图保持活动

为了获得所需的语义,我们需要传递一个标志来告诉系统:

PendingIntent pintent = 
   PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

此问题的问题来自以下行:

contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
最后一个参数不应为0,它应为pendingent.FLAG\u CANCEL\u CURRENT

contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

否则,在notificationIntent中,您将始终收到相同的数据

谢谢,这将更详细地解释Commonware Link描述的问题已经腐烂。这在理论上可以回答这个问题,但最好在这里为未来的用户提供答案的基本部分,并提供链接供参考。可以通过以下方式变得无效。