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

相关内容

热门资讯

推荐一款!凑一桌小程序辅助器,... 您好:凑一桌小程序辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
玩家交流!hhpoker这个软... 玩家交流!hhpoker这个软件靠谱吗,边锋老友修改器,分享教程(有挂透视)1、下载安装好hhpok...
总算了解!杭州都莱辅助软件下载... 您好:杭州都莱辅助软件下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
最新研发!雀姬手游辅助免费脚本... 最新研发!雀姬手游辅助免费脚本,上饶窝龙辅助,力荐教程(详细教程);无需打开直接搜索加(薇:1367...
玩家必用!多乐游戏小程序辅助器... 玩家必用!多乐游戏小程序辅助器免费,葫芦娃辅助脚本,攻略教程!(有挂方法)1、下载安装好多乐游戏小程...
新手必备!白金岛跑胡子脚本,小... 新手必备!白金岛跑胡子脚本,小闲川南怎么辅助,玩家教你(的确有挂);无需打开直接搜索加(薇:1367...
我来分享!广东雀伸辅助器,长了... 我来分享!广东雀伸辅助器,长了四人上品十三攻略,扑克教程(真的有挂)1、下载安装好广东雀伸辅助器,进...
一起来讨论!温州茶苑手机版辅助... 一起来讨论!温州茶苑手机版辅助器,微信决胜游戏辅助,力荐教程(发现有挂)1、下载安装好温州茶苑手机版...
总算清楚!中至余干六副牌有挂吗... 您好:中至余干六副牌有挂吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
推荐一款!创思维激k透视源码,... 推荐一款!创思维激k透视源码,杭州都莱辅助,新2024版(有挂教程);无需打开直接搜索加薇13670...