简体   繁体   English

应用程序终止后如何停止前台服务?

[英]How to stop a foreground service after app kill?

I'm using the foreground service with notification.我正在使用带有通知的前台服务。 I remove the service in the main activity onDestroy method by calling stopForeground and stopService.我通过调用 stopForeground 和 stopService 来删除主要活动 onDestroy 方法中的服务。 The problem is when I swipe my app of the recent apps to kill it, the debug session is still running and when I relaunch the application then the onCreate method of MyApplication extends with Application class not call.问题是当我刷我的最近应用程序的应用程序以将其杀死时,调试 session 仍在运行,当我重新启动应用程序时,MyApplication 的 onCreate 方法扩展为应用程序 class 不调用。

I see the answer here https://stackoverflow.com/a/53334788/10069542我在这里看到了答案https://stackoverflow.com/a/53334788/10069542

<service
android:enabled="true"
android:name=".ExampleService"
android:exported="false"
android:stopWithTask="true" />

When I write android:stopWithTask="true", it's working fine in android OS version 9 and below but not working in android OS version 10. It's weird when I write android:stopWithTask="false" it's working fine in android OS version 10 but not working any other version. When I write android:stopWithTask="true", it's working fine in android OS version 9 and below but not working in android OS version 10. It's weird when I write android:stopWithTask="false" it's working fine in android OS version 10但不适用于任何其他版本。

package com.example.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
private final String  VIDEO_SERVICE_CHANNEL = 
"VIDEO_SERVICE_CHANNEL";
public static final String TAG = "v_call";

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "MyService onCreate()");
    startForeground(1, getMyNotification("Riaz"));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "MyService onStartCommand()");
    return START_NOT_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.i(TAG, "MyService onTaskRemoved()");
    this.stopSelf();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "MyService onDestroy()");
    this.stopSelf();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    Log.i(TAG, "onLowMemory()");
}

private Notification getMyNotification(String userName){
    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(VIDEO_SERVICE_CHANNEL, getApplicationContext().getString(R.string.room_notification_channel_title), NotificationManager.IMPORTANCE_LOW);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
        }
    }
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    CharSequence title = getApplicationContext().getString(R.string.room_notification_title);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0,intent , 0);



    return new Notification.Builder(this,VIDEO_SERVICE_CHANNEL)
            .setContentTitle(title)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(contentIntent).build();
}}

Activity code活动代码

public class MainActivity extends AppCompatActivity {
Intent mServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    super.onStart();
    Log.i(MyService.TAG, "MainActivity onStart()");
    mServiceIntent = new Intent(MainActivity.this, MyService.class);
    startVideoService();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i(MyService.TAG, "MainActivity onDestroy()");
    stopService(mServiceIntent);
}

private void startVideoService() {
    Log.i(MyService.TAG, "startVideoService()");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(mServiceIntent);
    } else {
        startService(mServiceIntent);
    }

}}

Any ideas please?请问有什么想法吗?

Thanks in advance提前致谢

@Override public void onTaskRemoved(Intent rootIntent) { @Override public void onTaskRemoved(Intent rootIntent) {

    Intent intentz = new Intent(this, MapService.class);
    stopService(intentz);

    super.onTaskRemoved(rootIntent);
}

I had the same problem, solved it this way...我也遇到了同样的问题,就这样解决了。。。

@Override
public void onTaskRemoved(Intent rootIntent) {
   
   Intent intentz = new Intent(this, MapService.class);
   
   stopService(intentz);

   super.onTaskRemoved(rootIntent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 android 的通知中停止前台服务? - How to stop a foreground service from the notification in android? 如何知道 os 杀死我的前台服务 android - How to know os kill my foreground service android 关闭应用程序后如何停止意图服务 - How to stop intent service after closing app 强制停止我的包后,Android正在杀死我的前台服务 - Android is killing my foreground service after Force stop my package 运行前台服务时如何检测当前的前台应用程序 - How to detect current foreground app when foreground service is running 华为Y5在锁定模式10分钟后杀死前台服务 - Huawei Y5 kill the foreground service after 10 minutes on locked mode 按下“主页”后杀死应用程序(停止暂停即可停止应用程序) - Kill app after 'Home' is pressed (stop it from just pausing) 为什么前台服务从后台删除应用程序后无法正常工作? - Why foreground Service not working after removing app from the background? 杀死并重新启动应用程序后前台服务未停止 - Android Studio - Foreground service not stopping after killing and relaunching app - Android Studio Android-服务应用程序如何将KeyEvent调度到另一个前景应用程序 - Android - How can a Service App dispatch KeyEvents to another Foreground App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM