// 第一种情况,点击通知栏,不会出现两个相同Activity出现
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"来了一个通知", System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 预备式的Intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "消息的内容标题", "通知的内容.....",
contentIntent);
manager.notify(1, notification);
// 第二种情况,通知栏点击打开应用当前界面,不指定某个Activity
// 当点击通知的时候,当前应用当前界面。
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(getPackageName(),
"com.dashuai.music.qd.SplashActivity(第一个Activity,需要自信修改)"));
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
Intent.FLAG_ACTIVITY_CLEAR_TOP
);
本文介绍两种处理Android通知栏点击的方法:一是直接启动指定Activity并确保只显示一个实例;二是返回到应用首页,无论当前处于哪个界面。文中通过设置Intent标志位和PendingIntent实现功能。
1535

被折叠的 条评论
为什么被折叠?



