0

アクティビティを表示せずに、通知がタップされたときに発生するイベントを割り当てたい (ここで説明したように)。

どうやってやるの?

4

1 に答える 1

1

これを行うには、放送受信機を登録します。このようにして、通知がクリックされたときに、必要に応じてUIをまったく表示せずに、レシーバーでコードを実行できます。

基本的な受信機コード:

public class Provider extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        //Your code here

    }
}

マニフェスト:

<receiver android:name="Provider" />

通知のサンプルコード:

public static void showNotification(Context context) {
    CharSequence title = "title";
    CharSequence message = "text";

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(<image>, title, System.currentTimeMillis());
    notification.flags = Notification.FLAG_ONGOING_EVENT;

    Intent notificationIntent = new Intent(context, Provider.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, pendingIntent);
    notificationManager.notify(<notification id>, notification);
}
于 2013-01-19T16:40:47.713 に答える