应用未打开或关闭时的后台服务

Posted

技术标签:

【中文标题】应用未打开或关闭时的后台服务【英文标题】:Background service when app is not open or closed 【发布时间】:2021-02-09 08:50:46 【问题描述】:

我有一个位置服务,可以在 Firestore 中添加/更新数据。打开应用程序时它工作正常,但如果我强制关闭应用程序或切换到另一个应用程序,该服务将不再工作。我在 Realme 3 pro 设备上对此进行了测试。

    即使在应用管理器中关闭应用,也可以运行服务。

服务类

public class LocationService extends Service 
private static final String TAG = "SERVICE";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 0;
private static final float LOCATION_DISTANCE = 0;

private class LocationListener implements android.location.LocationListener 
    Location mLastLocation;

    public LocationListener(String provider) 
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    


    @Override
    public void onLocationChanged(Location location) 

        mLastLocation.set(location);
    

    @Override
    public void onProviderDisabled(String provider) 
        Log.e(TAG, "onProviderDisabled: " + provider);
    

    @Override
    public void onProviderEnabled(String provider) 
        Log.e(TAG, "onProviderEnabled: " + provider);
    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
        Log.e(TAG, "onStatusChanged: " + provider);
    


LocationListener[] mLocationListeners = new LocationListener[]
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
;

@Override
public IBinder onBind(Intent arg0) 
    return null;


@Override
public int onStartCommand(Intent intent, int flags, int startId) 
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    LocationInitialize();
    return START_STICKY;


@Override
public void onCreate() 
    Log.e(TAG, "onCreate");



public void LocationInitialize()
    initializeLocationManager();
    try 
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
     catch (java.lang.SecurityException ex) 
        Log.i(TAG, "fail to request location update, ignore", ex);
     catch (IllegalArgumentException ex) 
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    
    try 
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
     catch (java.lang.SecurityException ex) 
        Log.i(TAG, "fail to request location update, ignore", ex);
     catch (IllegalArgumentException ex) 
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    


@Override
public void onDestroy() 
    Log.e(TAG, "onDestroy");
    super.onDestroy();

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("restartservice");
    broadcastIntent.setClass(this, RestartService.class);
    this.sendBroadcast(broadcastIntent);


private void initializeLocationManager() 
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) 
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    


@Override
public void onTaskRemoved( Intent rootIntent ) 
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("restartservice");
    broadcastIntent.setClass(this, RestartService.class);
    this.sendBroadcast(broadcastIntent);


广播接收

public class RestartService extends BroadcastReceiver 

@Override
public void onReceive(Context context, Intent intent) 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        context.startForegroundService(new Intent(context, LocationService.class));
     else 
        context.startService(new Intent(context, LocationService.class));
    


Android 清单

<receiver
        android:name=".service.RestartService"
        android:enabled="true"
        android:stopWithTask="false"
        android:exported="true">
        <intent-filter>
            <action android:name="restartservice" />
        </intent-filter>
    </receiver>

    <service
        android:name=".service.LocationService"
        android:enabled="true"
        android:exported="true" />

【问题讨论】:

自从后台限制应用于android以来已经很长时间了。服务不能无限在后台运行。您需要为此使用前台服务。另请阅读Background Location Limits。 最好你可以去工作经理 【参考方案1】:

根据谷歌开发者最近的政策,你不应该使用 基于清单的广播接收器,虽然它不能在 26 以上工作 开发工具包

https://developer.android.com/guide/components/broadcasts#changes-system-broadcasts

所以不需要使用广播接收器来重启服务。

使用此代码重启服务

更新的服务类

public class LocationService extends Service 
private static final String TAG = "SERVICE";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 0;
private static final float LOCATION_DISTANCE = 0;

private class LocationListener implements android.location.LocationListener 
    Location mLastLocation;

    public LocationListener(String provider) 
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    


    @Override
    public void onLocationChanged(Location location) 

        mLastLocation.set(location);
    

    @Override
    public void onProviderDisabled(String provider) 
        Log.e(TAG, "onProviderDisabled: " + provider);
    

    @Override
    public void onProviderEnabled(String provider) 
        Log.e(TAG, "onProviderEnabled: " + provider);
    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
        Log.e(TAG, "onStatusChanged: " + provider);
    


LocationListener[] mLocationListeners = new LocationListener[]
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
;

@Override
public IBinder onBind(Intent arg0) 
    return null;


@Override
public int onStartCommand(Intent intent, int flags, int startId) 
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    LocationInitialize();
    return START_STICKY;


  @Override
    public void onCreate() 
        super.onCreate();      
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            startMyOwnForeground();
         else 
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "1");
            Notification notification = notificationBuilder.setOngoing(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("My service.")
                    .setPriority(NotificationManager.IMPORTANCE_MIN)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .build();
            startForeground(1, notification);
        
    

  @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground() 
        String channelName = "My service";
        NotificationChannel chan = new NotificationChannel("2", channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "2");
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My service")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    

public void LocationInitialize()
    initializeLocationManager();
    try 
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
     catch (java.lang.SecurityException ex) 
        Log.i(TAG, "fail to request location update, ignore", ex);
     catch (IllegalArgumentException ex) 
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    
    try 
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
     catch (java.lang.SecurityException ex) 
        Log.i(TAG, "fail to request location update, ignore", ex);
     catch (IllegalArgumentException ex) 
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    


@Override
public void onDestroy() 
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    // no need to do anything here
    //Intent broadcastIntent = new Intent();
    //broadcastIntent.setAction("restartservice");
    //broadcastIntent.setClass(this, RestartService.class);
    //this.sendBroadcast(broadcastIntent);


private void initializeLocationManager() 
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) 
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    


    @Override
    public void onTaskRemoved(Intent rootIntent) 
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);

        super.onTaskRemoved(rootIntent);
    

【讨论】:

如果我从最近的应用程序中删除应用程序,服务将停止。 你用的是什么设备? 好的,问题是我确实在清单中添加了后台服务。 有没有办法只在应用被最小化或从最近的应用中删除时才显示正在进行的通知? 我很高兴它成功了,别忘了为我的答案投票给其他人

以上是关于应用未打开或关闭时的后台服务的主要内容,如果未能解决你的问题,请参考以下文章

应用程序关闭时的 iOS 定位服务启用/禁用事件

android 怎样启动后台服务

后台上传大文件(应用关闭时服务重启)

Flutter Firebase Cloud Messaging - 应用程序在后台时的通知

arcgis未连接

即使浏览器关闭,服务人员也会在后台运行吗?