0

我使用创建了一个通知Notification.FLAG_ONGOING_EVENT。现在我想将它用作开/关开关,就像状态栏中的蓝牙或 wifi 一样。

我想将它用作我的服务的开关。如果我单击它,它将启动服务,如果我再次单击它,它将关闭服务。就像状态栏中的蓝牙/wifi或其他东西一样。因为我不能在状态栏上放任何东西,所以我想以这种方式使用通知栏。可能吗?

4

1 回答 1

0

使用待定意图,它将启动您的活动并添加额外内容以了解您是否必须打开/关闭。

// notification is clicked
Intent intent = new Intent(this, Main.class);
intent.putExtra(Main.SWITCH_ON, true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

oncreate 方法的附加包

Bundle extras = getIntent().getExtras();
        if(extras != null && extras.containsKey(SWITCH_ON) && extras.getBoolean(SWITCH_ON)) {
            launchMyMethod();
        }
于 2013-06-20T12:11:36.257 回答