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

相关内容

热门资讯

程序员教你!超凡辅助app链接... 程序员教你!超凡辅助app链接(白金岛手游放炮罚有挂)切实有作弊器开挂辅助工具(有挂猫腻);白金岛手...
玩家必看教程!518互游辅助(... 【福星临门,好运相随】;玩家必看教程!518互游辅助(如何做方片十三张脚本)从前有作弊器开挂辅助黑科...
玩家必看教程!新海豚辅助工具(... 玩家必看教程!新海豚辅助工具(微友助手破解版)好像有作弊挂开挂辅助神器(有挂秘笈);1、让任何用户在...
分享一款!新众亿软件可以安装(... 分享一款!新众亿软件可以安装(福州十八扑外卦)一向有作弊挂开挂辅助下载(今日头条);分享一款!新众亿...
实测教程!新财神正版辅助挂(游... 实测教程!新财神正版辅助挂(游戏辅助器免费版)其实有作弊挂开挂辅助插件(有挂辅助);游戏辅助器免费版...
玩家必看!新九哥智能辅助软件(... 玩家必看!新九哥智能辅助软件(小程序能不能开挂)确实有作弊器开挂辅助神器(有挂技巧);新九哥智能辅助...
信息共享!火神工作室辅助大全(... 信息共享!火神工作室辅助大全(朋友圈辅助软件)果然有作弊挂开挂辅助神器(有挂工具);一、火神工作室辅...
玩家必看科普!樱花之盛免费挂(... 玩家必看科普!樱花之盛免费挂(边锋老友辅助)固有有作弊挂开挂辅助脚本(有挂秘籍);亲真的是有正版授权...
总算清楚!天天微友辅助神器(上... 总算清楚!天天微友辅助神器(上饶打炸辅助)最初有作弊器开挂辅助工具(确实有挂);天天微友辅助神器 a...
科技新动态!微友辅助神器下载(... 科技新动态!微友辅助神器下载(人海大厅脚本)最初有作弊挂开挂辅助神器(有挂头条);科技新动态!微友辅...