
我在安装主题后发送通知.考虑我安装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.MiCreaseD/NewthemeChooser__:themeChangeReceiver( 4423): hascode : 2106806482 installed com.test.theme.iPhoneD/NewthemeChooser__:themeChangeReceiver( 4423): hascode : -1413669305 installed com.test.theme.SimpsonsD/NewthemeChooser__:themeChangeReceiver( 4423): hascode : -2146296452 installed com.test.theme.AnnathemeI/System.out( 4423): NewthemeChooser__:bundle apkID com.test.theme.MiCreaseI/System.out( 4423): NewthemeChooser__:bundle apkID com.test.theme.MiCreaseI/System.out( 4423): NewthemeChooser__:bundle apkID com.test.theme.MiCreaseI/System.out( 4423): NewthemeChooser__:bundle apkID com.test.theme.MiCrease解决方法 我遇到了同样的问题,问题是AndroID有点过于聪明,并且给你相同的PendingIntents而不是新的.从 docs:
A common mistake people make is to create multiple
PendingIntent@H_502_22@ objects withIntent@H_502_22@s that only vary in their “extra” contents,expecting to get a differentPendingIntent@H_502_22@ each time. This does not happen. The parts of theIntent@H_502_22@ that are used for matching are the same ones defined byIntent.filterEquals@H_502_22@. If you use twoIntent@H_502_22@ objects that are equivalent as perIntent.filterEquals@H_502_22@,then you will get the samePendingIntent@H_502_22@ for both of them.
修改代码如下,以提供唯一的requestCode:
// ...PendingIntent pendingIntent = PendingIntent.getActivity(mContext,packagename.hashCode(),0);// ...
这将确保使用唯一的PendingIntent,而不是相同的PendingIntent.
请注意,hashCode()可能不是唯一的,因此如果可能,请使用另一个唯一整数作为requestCode.
总结以上是内存溢出为你收集整理的Android多个通知在点击时发送相同的数据全部内容,希望文章能够帮你解决Android多个通知在点击时发送相同的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)