【问题标题】:Event on notification bar like Status bar android通知栏上的事件,如状态栏 android
【发布时间】:2013-06-17 06:45:29
【问题描述】:

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

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

【问题讨论】:

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

标签: android notifications statusbar android-notification-bar


【解决方案1】:

使用待定意图来启动您的活动并添加额外信息以了解您是否必须打开/关闭。

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

【讨论】:

  • 您是否使用过拨动开关来开启?实际上我想将通知本身用作开关。如果单击,它将提供午餐服务。如果再次单击它将停止服务。有没有类似的方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
相关资源
最近更新 更多