Android:检测状态栏通知单击
当我的应用程序触发通知时,我希望在用户单击通知后调用一个方法 以下是我创建通知的方式:Android:检测状态栏通知单击,android,notifications,Android,Notifications,当我的应用程序触发通知时,我希望在用户单击通知后调用一个方法 以下是我创建通知的方式: Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class); alarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); Bundle bundle = new Bundle();
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
PendingIntent pendingIntent;
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Date date = new Date();
bundle.putString("name", "name");
bundle.putString("body", "body");
alarmIntent.putExtras(bundle);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 10000, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setExact(AlarmManager.RTC_WAKEUP, date.getTime()+5000, pendingIntent); // fire after 5 seconds of launch
那么如何编辑我的代码来实现这一点。。谢谢请尝试此代码以获取通知 用于呼叫
notificationshow(this);
方法定义
notificationshow(Context c) {
/*********** Create notification ***********/
Intent intent = new Intent(this, act_run_ifclicknotifctn.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle("New Notification text title")
.setContentText("New content text")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
/*.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent)*/
.build();
NotificationManager NotiMgr = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
NotiMgr.notify(0, n);
}
令人惊叹的!这将启动act\u run\u ifclicknotifctn类,但是否可以启动主活动并调用方法?Thanksys当然可以尝试调用您自己的类myclass.class,如果您希望尝试在通知中定义布尔变量并使其为true,然后调用resume方法。检查变量是否为真执行方法如果为假则跳过它