事件通知栏,如状态栏android



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

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

使用一个Pending intent来启动你的activity,并添加额外的元素来知道你是否需要打开/关闭。

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

最新更新