繁体   English   中英

Android:检测状态栏通知单击

[英]Android: Detect status bar notification click

当我的应用程序触发通知时,我希望在用户单击通知后调用一个方法

这就是我创建通知的方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM