【问题标题】:How to stop a foreground service after app kill?应用程序终止后如何停止前台服务?
【发布时间】:2023-04-03 12:50:01
【问题描述】:

我正在使用带有通知的前台服务。我通过调用 stopForeground 和 stopService 来删除主要活动 onDestroy 方法中的服务。问题是当我刷我最近的应用程序以杀死它时,调试会话仍在运行,当我重新启动应用程序时,MyApplication 的 onCreate 方法扩展了 Application 类而不调用。

我在这里看到了答案 https://stackoverflow.com/a/53334788/10069542

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

当我编写 android:stopWithTask="true" 时,它在 android OS 版本 9 及更低版本中运行良好,但在 android OS 版本 10 中无法运行。 当我写 android:stopWithTask="false" 时很奇怪,它在 android OS 版本 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();
}}

活动代码

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

}}

有什么想法吗?

提前致谢

【问题讨论】:

  • 那个答案中提到的onTaskRemoved 怎么样?
  • 当我写 android:stopWithTask="false" 然后 onTaskRemoved 在所有版本中调用,它在 android OS 版本 10 但在任何其他版本中都可以正常工作,当我写 android:stopWithTask="true"然后 onTaskRemoved 未在 android OS 版本 9 及更低版本中调用,它在 android OS 版本 9 及更低版本中工作正常,但在 android OS 版本 10 中无法正常工作
  • 然后检查操作系统版本并确保它们都根据版本执行
  • 您是否在服务中返回START STICKY。您可以发布您的服务代码吗?
  • 我尝试使用 START STICKY 和 START_NOT_STICKY 返回但没有成功

标签: java android service android-notifications foreground-service


【解决方案1】:

@覆盖 public void onTaskRemoved(Intent rootIntent) {

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

    super.onTaskRemoved(rootIntent);
}

【讨论】:

  • 编辑您现有的答案,而不是将更新作为单独的答案发布。
【解决方案2】:

我遇到了同样的问题,就这样解决了……

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

   super.onTaskRemoved(rootIntent);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多