android fragment怎么调用onnewintent
发布网友
发布时间:2022-04-29 01:19
我来回答
共1个回答
热心网友
时间:2022-06-27 09:58
实现要点:主Activity必须设置android:launchMode为singleTask
<activity
android:name="cn.weixq.fscar.MainActivity"
android:launchMode="singleTask" >
</activity>
然后在生成通知栏消息时,要使用Intent传递参数,并且在主Activity中重写OnNewIntent事件来接收参数
// 生成通知栏消息
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.tip;
// 下面这个是通知刚显示时,在手机屏幕顶部显示的文字
notification.tickerText = "新违章提醒";
notification.when = System.currentTimeMillis();
// 使用所有默认值(声音+震动+灯光)
notification.defaults = Notification.DEFAULT_ALL;
// 用户点击通知后删除该通知
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 点击通知后要打开的layout(界面)
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
//传递参数
intent.putExtra("flag", "ok");
PendingIntent pendingIntent = PendingIntent.getActivity(SplashActivity.this,0,intent,0);
notification.setLatestEventInfo(SplashActivity.this, "[" + carNo + "]","有" + jsonArray.length()+ "条新违章,请到[我的车库]中查看!",pendingIntent);
// 创建并显示通知
notificationManager.notify((int) System.currentTimeMillis(),notification);
重写OnNewIntent事件
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
String flag = intent.getStringExtra("flag");
if (flag != null)
{
// 如果intent不为null,且flag的值为ok,则表示点击了通知栏消息,那么切换到车库界面
radioGroup.check(R.id.radioCar);
}
}