【问题标题】:allowing user to cancel foreground service允许用户取消前台服务
【发布时间】:2012-11-01 21:41:27
【问题描述】:

下面粘贴的代码显然 (1) 将我的服务设置为前台服务并 (2) 在屏幕顶部的托盘上显示我的图标。如果用户点击我的图标,今天的日期很快就会闪烁——我想这是某种默认行为。 ContentTitle 和 ContentText 从不显示(那么它们有什么用?)

我希望发生的情况是,当点击图标以显示某种下拉菜单或弹出窗口时,允许用户选择取消或结束我的服务。我似乎找不到任何此类功能的示例。这可以做到吗?怎么样?

顺便说一句,我的通知的唯一目的是让我的服务在前台运行。我从不需要通知用户任何事情。

谢谢,加里

NotificationCompat.Builder b = new NotificationCompat.Builder(this);

b.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("PhoneStalker")
.setContentText("Hello World");

Intent WhatIsThisIntentFor = new Intent(Intent.ACTION_VIEW);

b.setContentIntent(PendingIntent.getActivity(this, 0, WhatIsThisIntentFor, 0));

通知 n = b.build();

startForeground(5481, n);

【问题讨论】:

    标签: android service notifications


    【解决方案1】:

    这个可以吗?

    没有。然而,setContentIntent() 可以是一个广播 PendingIntent 到一个调用 stopService()BroadcastReceiver(或者任何你的机制来关闭你的服务)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2021-09-28
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多