几个小时后停止前台服务 [英] foreground services stopping after several hours

查看:68
本文介绍了几个小时后停止前台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用3个前台服务通过BluetoothLeScanner扫描和处理信标的应用程序.它们中只有一个具有扫描它们的代码,其他两个服务则处理扫描的信标并进行一些api调用.几个小时后,前台服务停止工作,并且通知消失.

I have an app that uses 3 foreground services to scan and process beacons with BluetoothLeScanner. Only one of them have the code to scan them, the other two services process the scanned beacons and make some api calls. After several hours the foreground services stops working and the notification dissappears.

通过ScanCallback onScanResult进行扫描.扫描每5分钟重新启动一次,以避免Android停止扫描.

The scanning is made through ScanCallback onScanResult. The scanning is restarted every 5 minutes to avoid Android stoping scanning.

我使用START_STICKY运行前台服务.我已经制作了一个广播接收器,该广播接收器在调用onDestroy()方法时重新启动服务.

I run the foreground services with START_STICKY. I've made a broadcast receiver that restarts the service when the onDestroy() method is called.

 @Override
    public void onCreate() {
        Log.i("EntryDetectionService", "Starting Service");
        super.onCreate();
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "Beacon Reader";
            String CHANNEL_NAME = "Scanning Beacons";

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setCategory(Notification.CATEGORY_SERVICE).setPriority(PRIORITY_HIGH)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .build();

            startForeground(101, notification);
        }
    }

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.getBluetoothScanner();
        mainLoopHandler.post(scan);

        return START_STICKY;
    }

    ScanSettings settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();

    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter filter = new ScanFilter.Builder()
             .setManufacturerData(76, new byte[] {})
             .build();
     filters.add(filter);

     if (bluetoothAdapter.isEnabled())
         bleScanner.startScan(filters, settings, scanCallback);

谢谢!

推荐答案

使APP保持活动状态当api> 21时,您可以使用 JobScheduler

Keep APP alive when api>21 , you can use JobScheduler

某些设备可能会使应用保持活动状态.

some devices may keep app live.

jobScheduler android示例代码

onStartJob 时检查服务是否仍然存在

when onStartJob check the service whether survive

public boolean isServiceRunning(Context context, String serviceName) {
        boolean isRunning = false;
        ActivityManager am = (ActivityManager) this
            .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo info : lists) {
            System.out.println(info.processName);
            if (info.processName.equals(serviceName)) {
                isRunning = true;
            }
        }
        return isRunning;

    }

这篇关于几个小时后停止前台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆