Android背景服务蓝牙扫描



我有一项服务来扫描蓝牙低能设备并显示通知。如果启动该应用程序或该应用在后台,则可以使用。但是,如果该应用程序从后台删除,则该服务正在运行,但是蓝牙扫描不起作用。如果该应用被杀死,服务可以做点什么吗?谢谢。

不清楚这个问题。

但根据我对上述问题的理解,请找到我的意见,如下所示。在极端情况下,如果Android系统需要内存,则基于该过程的重要性,它开始以最不重要的方式删除过程。在这种情况下,如果您的应用程序被杀死,则该应用程序的服务将不再继续运行。它也被杀死。

,但是可以将服务通过startforground()方法作为前景过程运行的过程。

如果应用程序的服务组件&整个应用程序都在两个不同的过程中运行&运行整个应用程序的过程被杀死,但是服务运行的过程仍在那里,然后需要检查诸如蓝牙adapter组件或诸如App Process& amp; amp; amp; amp;之类的依赖关系。确保该服务是在不同过程中运行的独立组件。

谢谢您的答案。问题在于应用上下文不可用。我找到了解决方案。我启动一个警报器,每15分钟发送一次意图。此意图由我自己的接收器处理,该接收器在AndroidManifest.xml中声明。如果处理意图,则可以使用应用程序上下文,我可以启动服务。

启动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);

接收器的代码

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) {
    }
}

在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>

最新更新