我有一个android服务,它经常获取位置并发送通知。尽管我进行了配置,以至于不能优化终端华为和小米的电池,但是在使用其他应用程序的GPS或拨打电话时,在奥利奥和英尺地区,我还是停止了服务。即使您具有OnStarCommand()START_STICKY,它也不会重新启动。这些削减之后,有什么方法可以自动重新启动该服务吗?我已经读到这些品牌在节电方面有非常积极的政策,而且我不知道这种情况是否会发生。 代码通知:
private Notification getNotification() {
Intent intent = new Intent(this, LocationUpdatesService.class);
text = Utils.dameCoordenadas(mLocation);
// Extra to help us figure out if we arrived in onStartCommand via the notification or not.
intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);
// The PendingIntent that leads to a call to onStartCommand() in this service.
PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// The PendingIntent to launch activity.
PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.addAction(R.drawable.ic_launch, getString(R.string.launch_activity),
activityPendingIntent)
.addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates),
servicePendingIntent)
.setContentText(text)
.setContentTitle(Utils.dameTituloNotificacionLocalizacion(this))
.setOngoing(true)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(text)
.setWhen(System.currentTimeMillis());
// Set the Channel ID for Android O.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID); // Channel ID
}
return builder.build();}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service started");
boolean startedFromNotification = intent.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
false);
if (startedFromNotification) {
removeLocationUpdates();
stopSelf();
}
return START_STICKY;}
答案 0 :(得分:0)
在Getting Location中,您可以找到用于oreo及以上设备的位置更新的最佳代码,它可在所有类型的设备(如mi,vivo和nokia)中使用。 试试这个,然后您在评论中提到的任何问题