10

android中的通知在点击时具有相同的意图。我在安装主题后发送通知。考虑我安装了 4 个主题和 4 个通知出现在通知窗口中,但是当我单击每个通知时,它会启动特定的活动,但每个意图的意图具有相同的数据。

我的代码是这样的

    @SuppressWarnings("deprecation")
void sendInstalledNotification(String fileName, String packageName) {
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    String name = "";
    try {
        name += fileName.substring(fileName.lastIndexOf(".") + 1);
    } catch (Exception e) {
        Log.e("NewThemeChooser", "Invalid Package name");
        e.printStackTrace();
    }
    name += " Installed";
    Notification notification = new Notification(R.drawable.ic_launcher_9, name , System.currentTimeMillis());

    Intent intent = new Intent(mContext , ThemeInfo.class);
    Bundle bundle = new Bundle();
    bundle.putString("apkid", packageName);
    bundle.putBoolean("isApplied", false);
    intent.putExtra("bundle", bundle);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
    notification.setLatestEventInfo(mContext, name, "Click to Apply Theme", pendingIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Log.d("NewThemeChooser__:ThemeChangeReceiver" , "hascode : " + packageName.hashCode() + " installed " + packageName);
    notificationManager.notify(packageName.hashCode(), notification);

}

我在 ThemeInfo 活动的 onCreate 中将意图数据打印为

    Bundle bundle = getIntent().getBundleExtra("bundle");
    apkid = bundle.getString("apkid");
    isApplied = bundle.getBoolean("isApplied", false);

    System.out.println("NewThemeChooser__:bundle apkid "  +  apkid );

我在日志中得到的结果是

D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -186637114 installed com.test.theme.MiCrease
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : 2106806482 installed com.test.theme.iPhone
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -1413669305 installed com.test.theme.Simpsons
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -2146296452 installed com.test.theme.AnnaTheme
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
4

2 回答 2

21

我遇到了同样的问题,问题是 Android 有点太聪明了,给你的是相同PendingIntent的 s 而不是新的。从文档

人们常犯的一个错误是使用 s 创建多个PendingIntent对象,这些对象仅在其“额外”内容上有所不同,并期望每次都Intent得到不同。PendingIntent这不会发生。Intent用于匹配的部分与 定义的部分相同Intent.filterEquals。如果您使用两个Intent根据 等价的对象Intent.filterEquals,那么您将获得相同PendingIntent的两个对象。

如下修改您的代码以提供唯一的requestCode

// ...
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0);
// ...

这将确保使用唯一PendingIntent的,而不是相同的。

请注意,它hashCode()可能不是唯一的,因此如果可能,请使用另一个唯一整数作为requestCode.

于 2013-06-19T20:29:32.737 回答
0

使用了这个对我有用的代码。

      int requestCode = new Random().nextInt();
      PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, 
      notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
于 2020-11-08T18:21:36.193 回答