[英]Foreground service continues to run after stopservice()
我正在閱讀有關前台服務的 android 文檔,它說:
要從前台刪除服務,請調用 stopForeground()。 該方法采用 boolean,表示是否也移除狀態欄通知。 請注意,該服務將繼續運行。
如果您在服務在前台運行時停止它,它的通知將被刪除。
在最后一段中,它說只有通知被刪除。
這意味着我不能完全停止服務? 系統會這樣做嗎?
還是我應該做點什么?
我使用這種方法來停止服務:
stopService(intent);
但是當通知被刪除時服務仍在運行。
這是簡單的服務:
public class MyService extends Service {
RemoteViews custom;
NotificationManager notificationmanager;
Notification notification;
IBinder mBinder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
notificationmanager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification_Channel(this);
startForeground(2,Show_notification(this));
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if(action!=null) {
switch (action) {
case "stop":
stopService(intent);
stopSelf();
break;
}
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@RequiresApi(api = Build.VERSION_CODES.S)
public void Notification_Channel (Context context) {
//creating channel
String offerchannelname = "...";
String offerchanneldescription = "...";
int offerchannel_importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notifchannel = new NotificationChannel( offerchannelid, offerchannelname,offerchannel_importance);
notifchannel.setDescription(offerchanneldescription);
// disable sound
notifchannel.setSound(null,null);
notificationmanager.createNotificationChannel(notifchannel);
// using custom Layout
custom = new RemoteViews(context.getPackageName(),R.layout.audio_player_notification);
Show_notification(context);
}
@RequiresApi(api = Build.VERSION_CODES.S)
public Notification Show_notification(Context context) {
PendingIntent pendingIntent;
Intent clickIntent = new Intent("action1");
pendingIntent = PendingIntent.getBroadcast(context,
3, clickIntent, PendingIntent.FLAG_IMMUTABLE| PendingIntent.FLAG_UPDATE_CURRENT);
custom.setOnClickPendingIntent(R.id.MyId,pendingIntent);
Intent clickIntent2 = new Intent("action2");
pendingIntent = PendingIntent.getBroadcast(context,
4, clickIntent2,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
custom.setOnClickPendingIntent(R.id.myId_player,pendingIntent);
Intent clickIntent3 = new Intent("action3");
pendingIntent = PendingIntent.getBroadcast(context,
5, clickIntent3,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
custom.setOnClickPendingIntent(R.id.myid,pendingIntent);
Intent open_intent = new Intent(context,Main_Page.class);
open_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent= PendingIntent.getActivity(context.getApplicationContext(),0,open_intent,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
return notification=new NotificationCompat.Builder(context,channel_id)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(true)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(custom)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build();
}
我使用這些方法來啟動我的服務:
Intent service_intent = new Intent(context,MyService.class);
ContextCompat.startForegroundService(context,service_intent);
context.bindService(service_intent, this,Context.BIND_AUTO_CREATE);
我使用這些方法來停止我的服務:
Intent service_intent = new Intent(context,MyService.class);
service_intent.setAction("stop");
ContextCompat.startForegroundService(context,service_intent);
context.unbindService(service_intent,this);
在我的 Logcat 中,出現了這一行
W/System:資源調用關閉失敗。
它沒有給我太多信息來找到原因。
正如我所看到的,您已經啟動了一個前台通知服務,但它永遠不會停止。 為了首先停止前台服務,您需要在 onStartCommand“停止”操作中使用 stopForeground(true) 關閉前台服務,然后您必須使用 stopSelf(startId) 來停止您的服務。
如需更多參考,請查看以下關於停止服務的 android 開發人員文檔。
https://developer.android.com/guide/components/services.html#Stopping[在此處輸入鏈接描述][1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.