Java 适用于Android 9及更高版本的FusedLocationProviderClient后台服务

Java 适用于Android 9及更高版本的FusedLocationProviderClient后台服务,java,android,location,android-service,fusedlocationproviderclient,Java,Android,Location,Android Service,Fusedlocationproviderclient,我尝试使用FusedLocationProviderClient每隔10秒获取一次位置,但是当我最小化应用程序或终止应用程序时,位置更新停止。下面是我的代码。现在我正在使用android 9.0。我不知道为什么会发生这种情况任何帮助都会很感激 安卓梅尼费斯特酒店 <service android:enabled="true" android:name=".LocationService"> </service> 您可以通过设置处理程序并向

我尝试使用FusedLocationProviderClient每隔10秒获取一次位置,但是当我最小化应用程序或终止应用程序时,位置更新停止。下面是我的代码。现在我正在使用android 9.0。我不知道为什么会发生这种情况任何帮助都会很感激

安卓梅尼费斯特酒店

        <service android:enabled="true" android:name=".LocationService">
        </service>

您可以通过设置处理程序并向android应用程序添加所需的权限来实现这一点。显然,你可以在后台请求位置的次数是有限制的,但我没有看到这一点。我每秒都在请求一个新的位置

首先将这些添加到您的清单中:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
createLocationHandler和isServiceRunning函数:

public void createLocationHandler(){
    HandlerThread locationHandler = new HandlerThread("LocationHandler"); //Creates a new handler thread
    locationHandler.start(); //Starts the thread
    Handler handler = new Handler(locationHandler.getLooper()); //Get the looper from the handler thread
    handler.postDelayed(new Runnable() {//Run the runnable only after the given time
        @Override
        public void run() {
            //Check if the location service is running, if its not. lets start it!
            if(!isMyServiceRunning(LocationService.class)){
                getApplicationContext().startService(new Intent(getApplicationContext(), LocationService.class));
            }
            //Requests a new location from the location service(Feel like it could be done in a less static way)
            LocationService.requestNewLocation();
            createLocationHandler();//Call the create location handler again, this will not be added to the stack because of the looper.
        }
    }, 10000);//Set the delay to be 10 seconds, 1 second = 1000 milliseconds
}

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) this.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
public void createLocationHandler(){
HandlerThread locationHandler=new HandlerThread(“locationHandler”);//创建一个新的处理程序线程
locationHandler.start();//启动线程
Handler Handler=new Handler(locationHandler.getLooper());//从处理程序线程获取循环器
postdayed(new Runnable(){//仅在给定时间后运行Runnable
@凌驾
公开募捐{
//检查位置服务是否正在运行,如果没有。让我们启动它!
如果(!isMyServiceRunning(LocationService.class)){
getApplicationContext().startService(新意图(getApplicationContext(),LocationService.class));
}
//从位置服务请求一个新位置(感觉可以用一种不太静态的方式完成)
LocationService.requestNewLocation();
createLocationHandler();//再次调用创建位置处理程序,由于循环器的原因,该处理程序不会添加到堆栈中。
}
},10000);//将延迟设置为10秒,1秒=1000毫秒
}
专用布尔值isMyServiceRunning(类serviceClass){
ActivityManager=(ActivityManager)this.getApplicationContext().getSystemService(Context.ACTIVITY_服务);
对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(service.service.getClassName())){
返回true;
}
}
返回false;
}

请记住,在启动服务之前,请先申请后台位置许可。

谢谢您的帮助。问题在于我的权限,我按照下面的url指南操作。我不知道安卓10引入了3个权限选项
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    createLocationHandler();
    ...
}
public void createLocationHandler(){
    HandlerThread locationHandler = new HandlerThread("LocationHandler"); //Creates a new handler thread
    locationHandler.start(); //Starts the thread
    Handler handler = new Handler(locationHandler.getLooper()); //Get the looper from the handler thread
    handler.postDelayed(new Runnable() {//Run the runnable only after the given time
        @Override
        public void run() {
            //Check if the location service is running, if its not. lets start it!
            if(!isMyServiceRunning(LocationService.class)){
                getApplicationContext().startService(new Intent(getApplicationContext(), LocationService.class));
            }
            //Requests a new location from the location service(Feel like it could be done in a less static way)
            LocationService.requestNewLocation();
            createLocationHandler();//Call the create location handler again, this will not be added to the stack because of the looper.
        }
    }, 10000);//Set the delay to be 10 seconds, 1 second = 1000 milliseconds
}

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) this.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}