简体   繁体   English

Android信标库后台服务-视频播放或加载某些API数据时的UI滞后

[英]Android beacon library Background service - UI lag when Video Play or Load some API Data

I'm developing a beacon application scanner (in Background when Bluetooth is activated) with Android Beacon Library . 我正在使用Android Beacon Library开发一个beacon应用程序扫描仪(激活蓝牙后在后台)。 The problem is whenever I activate the scanner in as a background, and I try to play a video with MediaController in another Activity or to load some API data, the video is not playing or sometimes it plays with lagging and the same as the API loading action. 问题是,每当我在后台激活扫描仪,并尝试在另一个Activity中使用MediaController播放视频或加载一些API数据时,该视频均无法播放,或者有时播放时滞并且与API加载相同行动。

But when I disable the scanner all works fine. 但是当我禁用扫描仪时,一切正常。

I don't know if the library runs its services in another thread or the same thread that blocks the UI, so I want to know how can I resolve this problem with multitask solution if it is possible. 我不知道该库是在另一个线程中还是在阻塞UI的同一线程中运行其服务,所以我想知道如果可能的话,如何使用多任务解决方案解决此问题。

I used huawei HOL-19 , API19 我用的是华为HOL-19,API19

this is the CustomApplication file: 这是CustomApplication文件:


import android.R
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.*
import android.util.Log
import com.example.anymarketmobile.views.subactivities.HomeActivity
import org.altbeacon.beacon.*
import org.altbeacon.beacon.powersave.BackgroundPowerSaver
import org.altbeacon.beacon.startup.BootstrapNotifier
import org.altbeacon.beacon.startup.RegionBootstrap

class BackgrounApplicationBeacons : Application(), BeaconConsumer, RangeNotifier {
    //  Init Properties
    lateinit var mBeaconManager: BeaconManager
    var mRegion: Region? = null
    var mMonitor = false
    var mLastDetectedBeacon: Beacon? = null
    val TAG = "==>"

    //  Actions & Callbacks
    override fun onBeaconServiceConnect() {
        mBeaconManager.addRangeNotifier(this)
        try {
            mBeaconManager.startRangingBeaconsInRegion(mRegion!!)
        } catch (e: RemoteException) {
        }
    }

    override fun didRangeBeaconsInRegion(beacons: MutableCollection<Beacon>?, region: Region?) {
        Log.i("==>Beacons:", "${beacons?.size}")
        if (beacons?.size!! > 0) {
            val beacon = beacons.iterator().next()
            if (beacon.id1 != mLastDetectedBeacon?.id1) {
                Log.i("${TAG}beacon detected:", "${beacon.id1} , ${beacon.distance} m")
                mLastDetectedBeacon = beacon
            }
        }
    }

    //  Start monitoring beacons
    fun startMonitoring() {
        mBeaconManager = BeaconManager.getInstanceForApplication(this)
        Log.i("==>", "Application launched:")
        if (mBeaconManager.isMainProcess) {
            mBeaconManager.beaconParsers.clear()
            mBeaconManager.beaconParsers.add(
                BeaconParser().setBeaconLayout(Constants.DEFAULT_BEACONS_LAYOUT)
            )
            mBeaconManager.setEnableScheduledScanJobs(false)
            val backgroundPowerSaver = BackgroundPowerSaver(this)
            mBeaconManager.backgroundBetweenScanPeriod = 0
            mBeaconManager.backgroundScanPeriod = 2100
            mRegion = Region("test", null, null, null)
            // mRegionBootstrap = RegionBootstrap(this, mRegion)
            mMonitor = true
            val builder = notificationBuilder()
            mBeaconManager.enableForegroundServiceScanning(builder.build(), 456)
            Log.i(TAG, "startMonitoring")
            mBeaconManager.applySettings()
            mBeaconManager.bind(this)

        }
    }

    //  Stop monitoring beacons
    fun stopMonitoring() {
        mRegion = null
        mBeaconManager.removeRangeNotifier(this)
        mBeaconManager.unbind(this)
        /*mRegionBootstrap?.disable()
        mRegionBootstrap = null*/
        Log.i(TAG, "stopMonitoring")
        mMonitor = false
        mLastDetectedBeacon = null
    }

    fun isMonitoring() = mMonitor


    //  Forground notification builder
    private fun notificationBuilder(): Notification.Builder {
        val builder = Notification.Builder(this)
        builder.setSmallIcon(R.drawable.ic_search_category_default)
        builder.setContentTitle("Scanning for Beacons")
        val intent = Intent(this, HomeActivity::class.java)
        intent.putExtra(Constants.FOREGROUND_SERVICE_COMMUNICATION_BEACON_SCAN, mMonitor)
        val pendingIntent = PendingIntent.getActivity(
            this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
        )
        builder.setContentIntent(pendingIntent)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "My Notification Channel ID",
                "My Notification Name", NotificationManager.IMPORTANCE_DEFAULT
            )
            channel.description = "My Notification Channel Description"
            val notificationManager = getSystemService(
                Context.NOTIFICATION_SERVICE
            ) as NotificationManager
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId(channel.id)
        }
        return builder
    }
}

I expected to avoid any lag or slowly execution. 我希望避免任何滞后或缓慢执行。

Much of the work of the Android bluetooth stack is on the UI thread -- all detection callbacks are made on the UI thread for example. Android蓝牙堆栈的大部分工作都在UI线程上-例如,所有检测回调都在UI线程上进行。 The Android Beacon Library maintains its own background thread pool and immediately switches detection processing to a lower-priority background thread once a packet comes in from the operating system. Android信标库会维护自己的后台线程池,一旦从操作系统传入数据包,便会立即将检测处理切换到优先级较低的后台线程。 As a result, the library processing overhead on the UI thread is minimal -- limited to just handing off work to a different thread for each packet seen. 结果,UI线程上的库处理开销很小-仅限于为看到的每个数据包将工作移交给另一个线程。

However, the operating system portion of the BLE detection processing is a different story, and some of it is done on the UI thread. 但是,BLE检测处理的操作系统部分是另一回事,其中一些是在UI线程上完成的。 I would be surprised if there were significant Android OS processing on the UI thread from a BLE scan unless a very large number of BLE devices were visible at the same time. 如果BLE扫描对UI线程进行大量的Android OS处理,除非同时可见大量BLE设备,否则我会感到惊讶。 I have not specifically tested this on a Huawei HOL-19, but I have tested it on stock Android devices (eg Pixel and Nexus devices) and confirmed the UI processing is minimal in cases where a dozen or fewer BLE devices are visible. 我尚未在华为HOL-19上进行过专门测试,但已在库存的Android设备(例如Pixel和Nexus设备)上进行了测试,并确认在可见到十二个或更少的BLE设备的情况下,UI处理最少。

You might try a different device to see if it might be a Huawei HOL-19 specific issue. 您可以尝试使用其他设备,查看是否可能是特定于华为HOL-19的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM