Android前台服务在几天后停止请求位置更新。
创始人
2024-10-09 15:02:12
0

出现这种情况可能是由于Android系统的Doze模式或应用程序被优化导致的。为了解决这个问题,我们可以使用AlarmManager在一定的时间间隔内重新启动服务,并在服务中请求位置更新。

以下是一个示例代码,其中“FOREGROUND_SERVICE_ID”是前台服务的ID,而“LOCATION_REQUEST_INTERVAL”是请求位置更新的时间间隔。

public class LocationService extends Service implements LocationListener {
 
    private static final long LOCATION_REQUEST_INTERVAL = 10000; // 10 seconds
 
    private LocationManager locationManager;
 
    private PendingIntent locationUpdateIntent;
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "default",
                    "Location Service",
                    NotificationManager.IMPORTANCE_LOW
            );
 
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
 
            Notification notification = new NotificationCompat.Builder(this, "default")
                    .setContentTitle("Location Service")
                    .setContentText("Location service is running")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
 
            startForeground(FOREGROUND_SERVICE_ID, notification);
        }
 
        requestLocationUpdates();
 
        return START_STICKY;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
 
        locationManager.removeUpdates(this);
    }
 
    private void requestLocationUpdates() {
 
        Intent intent = new Intent(this, LocationService.class);
        locationUpdateIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + LOCATION_REQUEST_INTERVAL, locationUpdateIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + LOCATION_REQUEST_INTERVAL, locationUpdateIntent);
        }
 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REQUEST_INTERVAL, 0, this);
 
    }
 
    @Override

相关内容

热门资讯

6分钟开挂!人海大厅挂件怎么买... 6分钟开挂!人海大厅挂件怎么买,来物局怎么开挂(2分钟细说开挂辅助软件);亲,人海大厅挂件怎么买这款...
发现透视"wepok... 发现透视"wepoker发牌算法"开挂(透视)辅助神器(玩家交流新版2026教程)发现透视"wepo...
四分钟辅助!山西奇迹打锅子辅助... 山西奇迹打锅子辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: ...
正版透视"wepok... 正版透视"wepoker是不是有人用挂"开挂(透视)辅助脚本(一分钟了解扑克教程)正版透视"wepo...
第3分钟开挂!潮汕木虱辅助下载... 第3分钟开挂!潮汕木虱辅助下载,中至余干破解器(分享开挂内幕开挂辅助安装)【无需打开直接搜索加薇13...
科技透视"wrpok... 科技透视"wrpoker透视挂真的吗"开挂(透视)辅助工具(揭秘一下曝光教程)【福星临门,好运相随】...
第2分钟开挂!圣盛鄂州晃晃辅助... 【亲,圣盛鄂州晃晃辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款圣盛鄂州晃晃辅助中打牌都会发...
正版透视"wepok... 正版透视"wepoker发牌的规律与打法技巧分享"开挂(透视)辅助挂(实测交流2026新版教程)《正...
四分钟辅助!510k中至余干辅... 四分钟辅助!510k中至余干辅助,指尖四川辅助脚本苹果版(我来教教大家开挂辅助神器);亲,510k中...
发现透视"wepok... 发现透视"wepoker发牌的规律与打法技巧分享"开挂(透视)辅助挂(终于知道爆料教程)【福星临门,...