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、打开软件启动之后找到中间准星...
技术分享!丰城麻将软件下载(透... 技术分享!丰城麻将软件下载(透视挂),丰城麻将果然有透视挂app1、下载好丰城麻将透视辅助下载之后点...
科技通报!微乐福建麻将可以装挂... 科技通报!微乐福建麻将可以装挂吗(透视挂),微乐福建麻将竟然真的有透视挂方式1、让任何用户在无需微乐...
玩家必看科普!洛阳麻将透视挂(... 玩家必看科普!洛阳麻将透视挂(透视挂),洛阳麻将本来存在有透视挂演示1、洛阳麻将破解器简单,洛阳麻将...
科技新动态!长城麻将外卦神器软... 科技新动态!长城麻将外卦神器软件(透视挂),长城麻将果然是有透视挂软件1、操作简单,无需长城麻将手机...
新手必备!明珠可以装挂吗(辅助... 新手必备!明珠可以装挂吗(辅助挂),明珠果然真的有辅助挂烘培1、金币登录送、破产送、升级送、活动送。...
发现玩家!中州棋牌辅助软件(开... 发现玩家!中州棋牌辅助软件(开挂),中州棋牌一直有开挂经验发现玩家!中州棋牌辅助软件(开挂),中州棋...
终于清楚!花城牌舍怎么开挂(透... 终于清楚!花城牌舍怎么开挂(透视),花城牌舍一贯存在有透视指引1、花城牌舍公共底牌简单,花城牌舍透视...