Android后台服务在华为已停止/暂停

时间:2019-03-22 09:44:13

标签: java android service background huawei


我开发了一个具有后台服务的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信标库的区域功能中用监视信标取代了蓝牙扫描。

1 个答案:

答案 0 :(得分:0)

您必须在设置中配置华为应用程序可以在后台运行

您可以查看此链接 here