![]() |
从容的圣诞树 · C#中等待异步函数执行完成,再继续向下执行代 ...· 1 年前 · |
![]() |
大方的铁板烧 · JPA快速上手指南—入门篇 - 知乎· 1 年前 · |
![]() |
深情的杯子 · 关于使用jq ...· 1 年前 · |
![]() |
玩足球的篮球 · 如何将列表和元组转化为数组_元组转换为数组_ ...· 2 年前 · |
我试图停止作为前台服务运行的服务。
当前的问题是,当我调用
stopService()
时,通知仍然保持不变。
因此,在我的解决方案中,我添加了一个接收器,我在
onCreate()
中注册它
在
onReceive()
方法中,我调用
stopforeground(true)
,它隐藏通知。然后
stopself()
停止服务。
在
onDestroy()
内部,我取消了接收器的注册。
有更合适的方法来处理这件事吗?因为stopService()根本无法工作。
@Override
public void onDestroy(){
unregisterReceiver(receiver);
super.onDestroy();
}
发布于 2013-12-31 12:22:58
startService(intent)
并传递一些数据,表示
停止服务
的键。
stopForeground(true)
stopSelf()
就在后面。
发布于 2018-06-16 14:02:25
要从活动中启动和停止前台服务,请使用:
//start
Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(startIntent);
//stop
Intent stopIntent = new Intent(MainActivity.this, ForegroundService.class);
stopIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
startService(stopIntent);
在前台服务中--使用(至少)以下代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
// your start service code
else if (intent.getAction().equals( Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
//your end servce code
stopForeground(true);
stopSelfResult(startId);
return START_STICKY;
![]() |
大方的铁板烧 · JPA快速上手指南—入门篇 - 知乎 1 年前 |