簡體   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