gpt4 book ai didi

Android 多重通知在点击时发送相同的数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:55 26 4
gpt4 key购买 nike

Android 中的通知对点击具有相同的 Intent 。我在安装主题后发送通知。假设我安装了 4 个主题,4 个通知出现在“通知”窗口中,但是当我单击每个通知时,它会启动特定的 Activity ,但每个 Intent 的 Intent 都具有相同的数据。

我的代码是这样的

    @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 Activity 的 onCreate 中将 Intent 数据打印为

    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

最佳答案

我有同样的问题,问题是 Android 有点太聪明了,给你相同的 PendingIntent 而不是新的。来自 docs :

A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

按如下方式修改您的代码以提供唯一的requestCode:

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

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

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

关于Android 多重通知在点击时发送相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17023839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com