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

相关内容

热门资讯

分析开挂!随意玩5元流量包,智... 您好:随意玩5元流量包这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
正版开挂!新天天贵阳麻将辅助工... 正版开挂!新天天贵阳麻将辅助工具,桂麻圈辅助(盘点一款开挂辅助神器)>>您好:软件加薇1367043...
细节辅助!新蜜瓜大厅控制器,随... 您好:新蜜瓜大厅控制器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
原来有辅助!雀友会辅助潮汕麻将... 原来有辅助!雀友会辅助潮汕麻将脚本,皮皮游戏辅助平台(重大通报开挂辅助安装);打开点击测试直接进入微...
正品开挂!悟空大厅辅助,多多科... 正品开挂!悟空大厅辅助,多多科技手游辅助(透视数据开挂辅助脚本);一、多多科技手游辅助AI软件牌型概...
实测开挂!财神13张辅助器下载... 实测开挂!财神13张辅助器下载,潮友会鱼虾蟹塞子概率计算方式(今日科普开挂辅助工具);无需打开直接搜...
传授辅助!广东雀用的是什么智能... 佛手在线破解 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、软...
盘点开挂!九哥玩挂,天道联盟辅... 盘点开挂!九哥玩挂,天道联盟辅助软件(透视私人局开挂辅助软件)天道联盟辅助软件ai黑科技系统规律教程...
正版辅助!雀神挂件价格辅助开挂... 正版辅助!雀神挂件价格辅助开挂,wepoker内置辅助器(透视模拟器开挂辅助工具)您好:雀神挂件价格...
分析辅助!新道游开挂,山西扣点... 您好:山西扣点辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...