简体   繁体   English

Android后台服务蓝牙扫描

[英]Android Background Service Bluetooth Scan

I have a service to scan Bluetooth Low Energy devices and show a notification. 我有一项服务可以扫描低功耗蓝牙设备并显示通知。 This works if the app is started or the the app is in background. 如果该应用已启动或该应用处于后台,则此方法有效。 But if the app was removed from background, the service is running but the bluetooth scan dont work. 但是,如果该应用已从后台删除,则该服务正在运行,但蓝牙扫描无法正常工作。 Can a service do something if the app is killed? 如果应用被杀死,服务可以做些什么吗? Thank you. 谢谢。

Not clear about the question. 这个问题不清楚。

But based on my understanding about the above question, please find my inputs as follows. 但是根据我对上述问题的理解,请按以下说明找到我的意见。 In extreme case if the Android system requires the memory, based on the importance of the processes it starts removing the processes in the order of least significant way. 在极端情况下,如果Android系统需要内存,则基于进程的重要性,它会以最小有效的顺序开始删除进程。 In this case if your app gets killed then there is no way the service from that app will continue to run. 在这种情况下,如果您的应用程序被杀死,则该应用程序的服务将无法继续运行。 It also gets killed. 它也被杀死。

But one can put the process in which the service is running as Foreground process by startForground() method by which it is less likely gets killed. 但是可以通过startForground()方法将服务在其中运行的进程作为前台进程运行,这种方法不太可能被杀死。

If service component of the application & the whole application are running in two different processes & the process running the whole app gets killed but the process with the service running is still there then one need to check the dependencies such as BluetoothAdapter component or such things got killed by the app process & make sure that the service is stand alone component running in different process. 如果应用程序的服务组件和整个应用程序在两个不同的进程中运行,并且运行整个应用程序的进程被杀死,但是运行服务的进程仍然存在,则需要检查诸如BluetoothAdapter组件之类的依赖项被应用程序进程终止并确保该服务是在不同进程中运行的独立组件。

Thank you for the answer. 谢谢你的回答。 The problem was that the Application Context was not available. 问题是应用程序上下文不可用。 I found a solution for this. 我找到了解决方案。 I start a Alarmmanager and send every 15 minutes an intent. 我启动一个Alarmmanager,每15分钟发送一次意图。 This intent is handled by my own receiver which is declared in the AndroidManifest.xml. 此意图由我自己的接收者处理,该接收者在AndroidManifest.xml中声明。 If the intent is handled, the Application Context is available and i can start the service. 如果意图得到处理,则可以使用应用程序上下文,并且我可以启动服务。

Start AlarmManager 启动AlarmManager

Intent alarmIntent = new Intent(MyApp.getAppContext(),AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MyApp.getAppContext(), 0, alarmIntent, 0);
AlarmManager alarmMgr = (AlarmManager)MyApp.getAppContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        AlarmManager.INTERVAL_FIFTEEN_MINUTES,
        AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

Code for the Receiver 接收方代码

package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {

    }

}

Declare Receiver in AndroidManifest.xml 在AndroidManifest.xml中声明接收器

<receiver 
        android:enabled="true" 
        android:name="com.example.AlarmBroadcastReceiver"
        android:exported="false">
        <intent-filter>
                <action android:name="com.example.AlarmBroadcastReceiver.checkservice" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

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

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