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

相关内容

热门资讯

一起来探讨!凑一桌游戏关春天辅... 凑一桌游戏关春天辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加...
如何分辨真伪!微乐游戏竞技团辅... 如何分辨真伪!微乐游戏竞技团辅助,wepoker安装教程,大神讲解(有挂分享)-哔哩哔哩 【无需打开...
揭秘!宝宝浙江游戏免费开挂,猎... 揭秘!宝宝浙江游戏免费开挂,猎鱼达人破解版无限弹头,第三方教程(有挂技术)-哔哩哔哩;亲,宝宝浙江游...
透视总结!潮友会鱼虾蟹辅助,战... 透视总结!潮友会鱼虾蟹辅助,战神辅助器,揭秘教程(有挂详细)-哔哩哔哩;无需打开直接搜索加薇1367...
今日焦点!互游辅助,大懒人斗十... 今日焦点!互游辅助,大懒人斗十四辅助,线上教程(真是有挂)-哔哩哔哩;最新版2026是一款经典耐玩的...
透视脚本!潮汕汇游戏辅助,we... 潮汕汇游戏辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,潮汕汇游戏辅助作为一种经典的娱乐方式,...
透视智能ai!牛总管辅助免费版... 透视智能ai!牛总管辅助免费版,欢聚水鱼脚本,细节揭秘(有挂头条)-哔哩哔哩>>您好:软件加1367...
我来教大家!天酷互娱有辅助工具... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
透视辅助!美猴王房卡辅助,wp... 透视辅助!美猴王房卡辅助,wpk安卓下载辅助,科技教程(有挂方法)-哔哩哔哩 了解更多开挂安装加(1...
重大通报!功夫川嘛辅助器是真的... 重大通报!功夫川嘛辅助器是真的假的,三加一免费辅助,软件教程(有挂存在)-哔哩哔哩;无需打开直接搜索...