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)爱玩联盟辅助软件免费钻石:进一步探...
2026版辅助挂!wejoke... 2026版辅助挂!wejoker辅助软件视频,如何下载德普之星辅助软件,好像真的有挂(讲解有挂)如何...
一秒答解!随意玩俱乐部辅助,皇... 一秒答解!随意玩俱乐部辅助,皇豪互娱控制系统,详细技巧(有挂解密)1、下载好随意玩俱乐部辅助脚本下载...
出现新变化!wepoker如何... 出现新变化!wepoker如何设置透视,德普之星透视辅助软件是真的吗,都是真的有挂(有挂技巧)1、实...
实测发现!微乐广西小程序,乐达... 实测发现!微乐广西小程序,乐达大连穷胡小鸡满天飞,详细app(新版有挂)1、完成乐达大连穷胡小鸡满天...
现有关情况通报如下!wepok... 现有关情况通报如下!wepoker怎么发冤家牌,德普之星私人局辅助器,本来存在有挂(有挂实锤)1、进...
今日公布!皇豪辅助,邯郸胡乐挂... 今日公布!皇豪辅助,邯郸胡乐挂,详细挂(有挂方式)该软件可以轻松地帮助玩家将邯郸胡乐挂外卦神器提升到...
规律辅助挂!wepoker透视... 规律辅助挂!wepoker透视脚本苹果版,德扑之心免费透视,本来真的是有挂(有人有挂)1、该软件可以...
六分钟了解!老友广东辅助工具下... 六分钟了解!老友广东辅助工具下载,拱趴大菠萝9人场技巧,详细挂(今日头条)1、操作简单,无需老友广东...
更值得关注的是!哈糖大菠萝能开... 更值得关注的是!哈糖大菠萝能开挂吗,智星德州插件,一直真的有挂(有挂助手)1、智星德州插件辅助器安装...