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

相关内容

热门资讯

WPK下载教程书!智星德州插件... WPK下载教程书!智星德州插件2024最新版,AApokerai多功能辅助器插件1、智星德州插件20...
WPK多开绝活!德州圈脚本,红... WPK多开绝活!德州圈脚本,红龙扑克ai多功能开挂攻略1、进入游戏-大厅左侧-新手福利-激活码辅助透...
WPK模拟器经验!德普之星辅助... WPK模拟器经验!德普之星辅助器怎么用,wpkai多功能辅助器软件1、操作简单,无需德普之星辅助器怎...
WPK购买步骤!约局吧德州真的... WPK购买步骤!约局吧德州真的有透视挂吗,德州扑克ai多功能透视教程1、完成约局吧德州真的有透视挂吗...
WPK有透视绝活儿!AApok... WPK有透视绝活儿!AApoker万能,wpkai多功能开挂脚本该软件可以轻松地帮助玩家将AApok...
WPK四人演示!德普之星的辅助... WPK四人演示!德普之星的辅助工具介绍,微扑克ai多功能透视挂插件德普之星的辅助工具介绍能透视中分为...
WPK透视版方式!微扑克辅助下... WPK透视版方式!微扑克辅助下载,智星菠萝德州ai多功能透视脚本1、让任何用户在无需微扑克辅助下载安...
WPK软件策略!德扑软件,AA... WPK软件策略!德扑软件,AApokerai多功能开挂技巧1)德扑软件辅助插件:进一步探索德扑软件辅...
WPK下载手段!wpk辅助是什... WPK下载手段!wpk辅助是什么,微扑克ai多功能开挂方法wpk辅助是什么脚本下载中分为三种模型:w...
WPK安卓演示!aapoker... WPK安卓演示!aapoker俱乐部靠谱吗,菠萝智星德州ai多功能辅助攻略1、超多福利:超高返利,海...