我有一项服务可以扫描低功耗蓝牙设备并显示通知。如果应用程序已启动或应用程序在后台,则此方法有效。但是如果应用程序从后台删除,服务正在运行,但蓝牙扫描不起作用。如果应用程序被杀死,服务可以做些什么吗?谢谢你。
2 回答
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. 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.
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.
谢谢你的回答。问题是应用程序上下文不可用。我找到了解决方案。我启动了一个 Alarmmanager,每 15 分钟发送一次意图。这个意图由我自己的接收器处理,它在 AndroidManifest.xml 中声明。如果处理了意图,则应用程序上下文可用,我可以启动服务。
启动报警管理器
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>