Android:检测状态栏通知单击

时间:2015-06-13 20:50:22

标签: 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();
    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

那么如何编辑我的代码来实现这一点..谢谢

1 个答案:

答案 0 :(得分:1)

尝试使用此代码进行通知

致电

    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);
    }