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

相关内容

热门资讯

今年以来!好玩贰柒柒辅助软件,... 今年以来!好玩贰柒柒辅助软件,红黑大战控制系统(透视)脚本-果然存在有挂1、让任何用户在无需好玩贰柒...
今天下午!顺欣茶楼辅助视频,友... 今天下午!顺欣茶楼辅助视频,友友联盟免费辅助器(透视)教程-总是存在有挂友友联盟免费辅助器脚本下载中...
目前来看!赣牌圈有挂吗,山西扣... 目前来看!赣牌圈有挂吗,山西扣点带你辅助工具破解(透视)软件-一贯有挂1、山西扣点带你辅助工具破解免...
据报道!萍乡滚筒四幅攻略,奇迹... 据报道!萍乡滚筒四幅攻略,奇迹陕西辅助器(透视)软件-确实是真的挂1.萍乡滚筒四幅攻略 选牌创建新账...
值得注意的是!微乐陕西小程序破... 值得注意的是!微乐陕西小程序破解器下载,途游辅助软件(透视)工具-一贯真的有挂1、很好的工具软件,可...
今年以来!欢乐达人脚本,多乐跑... 您好,多乐跑得私人房间插件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
今天下午!麻友圈安庆版插件,赖... 今天下午!麻友圈安庆版插件,赖子三加一辅助(透视)神器-一直是有挂1、起透看视 麻友圈安庆版插件辅助...
备受关注的!微信财神十三章辅助... 备受关注的!微信财神十三章辅助,爱玩联盟辅助下载(透视)脚本-总是真的有挂1、在微信财神十三章辅助插...
有消息称!天天爱消除辅助脚本,... 有消息称!天天爱消除辅助脚本,四川游戏家园辅助软件下载(透视)教程-果然存在有挂1、在四川游戏家园辅...
据悉!微乐自建房辅助可信吗,开... 据悉!微乐自建房辅助可信吗,开心泉州小程序有挂吗(透视)方法-确实真的有挂一、微乐自建房辅助可信吗可...