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工具手册!AApoker... WPK工具手册!AApoker使用,菠萝智星德州ai多功能透视方法进入游戏-大厅左侧-新手福利-激活...
WPK德州局指南!fishpo... WPK德州局指南!fishpoker透视,AApokerai多功能辅助工具fishpoker透视透视...
WPK透视版绝活儿!哈糖大菠萝... WPK透视版绝活儿!哈糖大菠萝有挂吗,菠萝智星德州ai多功能辅助插件1、玩家可以在哈糖大菠萝有挂吗透...
WPK插件策略!扑克之星辅助,... WPK插件策略!扑克之星辅助,红龙扑克ai多功能辅助器技巧扑克之星辅助透视方法中分为三种模型:扑克之...
WPK软件手册!AApoker... WPK软件手册!AApoker拿好牌,红龙扑克ai多功能辅助器教程1、金币登录送、破产送、升级送、活...
WPK安卓资料!佛手在线大菠萝... WPK安卓资料!佛手在线大菠萝技巧,智星菠萝德州ai多功能透视挂app1、每一步都需要思考,不同水平...
WPK四人项目!德普之星的辅助... WPK四人项目!德普之星的辅助工具介绍,AApokerai多功能透视神器德普之星的辅助工具介绍辅助器...
WPK四人策略!wpk德州局透... WPK四人策略!wpk德州局透视,wpkai多功能辅助插件1、完成wpk德州局透视辅助器v3.3的残...
WPK官网模板!AApoker... WPK官网模板!AApoker插件,WePoKerai多功能透视挂app1、实时AApoker插件透...
WPK官网法子!aapoker... WPK官网法子!aapoker透视脚本,红龙PoKerai多功能透视插件1、下载好aapoker透视...