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

前端之家收集整理的这篇文章主要介绍了Android多个通知在点击时发送相同的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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,intent,0);
    notification.setLatestEventInfo(mContext,"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);

}

我正在onCreate of ThemeInfo活动中打印意图数据

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有点过于聪明,并且给你相同的PendingIntents而不是新的.从 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(),0);
// ...

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

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

原文链接:/android/316958.html

猜你在找的Android相关文章