几个小时后停止前台服务

编程入门 行业动态 更新时间:2024-10-27 22:29:07
本文介绍了几个小时后停止前台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用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; }

更多推荐

几个小时后停止前台服务

本文发布于:2023-11-27 02:03:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   前台   小时后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!