Android 用于区分状态栏中的通知和启动主活动的通知的任何方法
我的应用程序服务中有两个通知代码 其中一个通知用于发送消息Android 用于区分状态栏中的通知和启动主活动的通知的任何方法,android,notifications,statusbar,click,Android,Notifications,Statusbar,Click,我的应用程序服务中有两个通知代码 其中一个通知用于发送消息 if (NOTIFICATION_COUNT_Message > 0){ Log.d("tag"," message notification start"); NotificationCompat.Builder mbuild = new NotificationCompat.Builder(getApplicationConte
if (NOTIFICATION_COUNT_Message > 0){
Log.d("tag"," message notification start");
NotificationCompat.Builder mbuild = new NotificationCompat.Builder(getApplicationContext());
mbuild.setSmallIcon(R.drawable.sms_on_click);
mbuild.setContentText(NOTIFICATION_COUNT_Message +" New Message");
Intent in = new Intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),1234,in,0);
mbuild.setContentIntent(resultIN);
NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=mbuild.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
nmagr.notify(1234,notification);
shr.edit().putString(MainActivity.KEY_ROOT_MESSAGE, "getMessage").commit();
// mbuild.getNotification().flags |= Notification.FLAG_AUTO_CANCEL ;
}
第二个通知代码是
if (NOTIFICATION_COUNT > 0){
Log.d("tag","notification start ");
NotificationCompat.Builder mbuild = new NotificationCompat.Builder(getApplicationContext());
mbuild.setSmallIcon(R.drawable.images1);
mbuild.setContentText(NOTIFICATION_COUNT +" New Message");
Intent in = new Intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),code,in,0);
mbuild.setContentIntent(resultIN);
NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=mbuild.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
nmagr.notify(code,notification);
// mbuild.getNotification().flags |= Notification.FLAG_AUTO_CANCEL ;
shr.edit().putString(MainActivity.KEY_ROOT_NOTIFY, "getAnecMessage").commit();
}
我的问题是如何处理这些通知的单击事件
在这两个通知中,我的主要活动将按我使用的方式启动
Intent in=new Intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP)
PendingEvent resultIN=PendingEvent.getActivity(getApplicationContext(),代码,in,0);mbuild.setContentIntent(结果);
。。出现的问题是,它无法区分活动是从哪个通知开始的,因此可能会执行更多的代码
因此,我主要想知道是否有任何方法可以识别启动主要活动的通知
我使用了公共共享首选项键。。但是它被覆盖了。。。因此,无论如何,这都不是解决方案
两个通知中使用的共享首选项代码具有不同的键也没有帮助,因为两个通知代码并行工作。所以这两个键都将被更新
shr.edit().putString(MainActivity.KEY_ROOT_NOTIFY,“GetAneceMessage”).commit()
shr.edit().putString(MainActivity.KEY_ROOT_MESSAGE,“getMessage”).commit()
因此,我可以看到我的问题的一个解决方案,即状态栏上两个通知的onclick方法,它可以区分哪个通知访问了主通知
单击时,将为两个不同的通知执行活动和单独的代码。我得到了问题的答案:D 我只是试了一下,效果不错 对于服务中全局声明的通知,我使用了一个共同的意图 在传递挂起的intent中的intent之前,我使用intent.putextra分配了一个字符串。。。nd now程序对两个通知的响应不同 这是第一个通知代码:
// in is intent declared globally in the service
in = new intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
**in.putExtra("METHOD", "getAnecMessage");** // added this code
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),code,in,0);
in = new Intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.putExtra("METHOD", "getMessage");
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),1234,in,0);
这是第二个通知代码:
// in is intent declared globally in the service
in = new intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
**in.putExtra("METHOD", "getAnecMessage");** // added this code
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),code,in,0);
in = new Intent(getApplicationContext(),MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.putExtra("METHOD", "getMessage");
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),1234,in,0);
用于在一个键中从两个不同的通知中相应地传递两个不同的值,然后
现在,当我点击通知时,它打开main,一个bundle检查是否有任何意图启动了主活动
如果它是由来自应用程序任何部分的通知意图启动的
捆绑包检查main中的代码,如下所示:
Bundle in = getIntent().getExtras();
if(in!=null){
str = in.getString("METHOD");
if(str.equals("getMessage")==true){
Log.d("tag Message","lanuch get message fragment");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSlidingMenu().toggle();
getSupportActionBar().setTitle("Messages");
Messages msg=new Messages();
ft.replace(R.id.details, msg);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
if(str.equals("getAnecMessage")==true){
Log.d("tag Notification", "launch getanec message activity") ;
NotificationAdapter adapter=new NotificationAdapter(MainActivity.this, notification_data);
if (adapter!=null) {
list=(ListView)slidingMenu.findViewById(R.id.list_notification_menu);
list.setAdapter(adapter);
}
}
因此,区分哪个通知启动了主要活动的问题得到了解决:D!!!!
我希望这也能帮助你 您的两个首选项值不同,对吗。