java - Android后台服务在华为停止/暂停
问题描述
我开发了一个 Android 应用程序,它有一个无休止地运行的后台服务,并将蓝牙扫描和 GPS 位置的结果保存在本地 SqLite DB 上。仅在华为设备上,此服务似乎暂停或停止了几分钟(我注意到在将一些日志插入代码后):在这些分钟内,任何日志都被写入。. 我尝试更改设备的某些设置(电池优化)但没有成功。您有解决问题的建议吗?
您可以在下面找到该服务的片段。
public class MyService extends Service {
public MyService() {
super();
}
@Override
public void onCreate() {
super.onCreate();
...
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1031, getNotification());
}
final Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
MyServiceBinder binder = (MyServiceBinder) service;
started = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
started = false;
}
};
bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
}
@RequiresApi(Build.VERSION_CODES.O)
private Notification getNotification() {
NotificationChannel channel = new NotificationChannel("channel_01", "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(getApplicationContext(), "channel_01");
builder.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText(getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_stat_onesignal_default);
return builder.build();
}
public class MyServiceBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
private void stopForegroundService()
{
// Stop foreground service and remove the notification.
stopForeground(true);
// Stop the foreground service.
stopSelf();
}
@Override
public void onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForegroundService();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Restart after 5 secs
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
@Override
public void run() {
GenericUtility.launchService(MyService.class, getApplication());
}
}, 5000);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
...
initScanLoop();
initLocationManager();
return Service.START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyServiceBinder();
}
@Override
public boolean onUnbind(Intent intent) {
boolean res = super.onUnbind(intent);
return res;
}
/*Init bluetooth handler*/
private void initScanLoop() {
final Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
scanLeDevice();
hBeacon.postDelayed(this, SCAN_DURATION + 10000);
}
});
}
private void scanLeDevice() {
if(mLEScanner != null && !scanning.get() && !stopScan) {
scanning.set(true);
mLEScanner.startScan(null, settings, mScanCallback);
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if(scanning.get()) {
stopScanLeDevice();
}
}
}, SCAN_DURATION);
}
}
private void stopScanLeDevice() {
scanning.set(false);
if(mLEScanner != null) {
mLEScanner.stopScan(mScanCallback);
}
}
/*Finish bluetooth handler*/
/*Init GPS handler*/
private void initLocationManager() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
createLocationChangedCallback();
locationListener = new BeaconScanLocationListener(locationChangedCallback);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
private void createLocationChangedCallback() {
locationChangedCallback = new LocationChangedCallback() {
@Override
public void callback(final Location location) {
try {
//GPS callcback
} catch(Exception e) {
}
}
@Override
public void enabledDisabled(boolean enabled) {
}
};
}
/*Finish GPS handler*/
}
更新
我改进了应用程序功能,在 Android 信标库的区域功能中用监控信标替换蓝牙扫描。
解决方案
您必须在设置中配置应用程序才能在华为后台运行
你可以在这里查看这个链接
推荐阅读
- nestjs - 捆绑一个 NestJS + TypeORM 应用程序(使用 webpack)
- firebase - 用户私有 Cloud Firestore 的 API
- python - 在 Keras 中使用双向 LSTM 填空句子
- python - 使用 for 循环中的值从 txt 创建字典列表
- reactjs - ReactJS 动态遍历对象并显示 4 列
- python - 如何忽略数据框中的空列?(熊猫)
- python - Python ModuleNotFoundError:没有名为“google.analytics”的模块
- python - VC2019 的开发者 shell 中的 pip install 产生“错误:需要 Microsoft Visual C++ 14.0 或更高版本。” 错误
- html5-canvas - Angular 10 - Chart.js 在运行时使用 convas id 在数组中绘制图表
- reactjs - “无法从...安装,因为它不包含 package.json 文件”通过 npx create-react-app app-name 在 Windows 10 中创建反应应用程序时