位置为空融合位置提供者android

Posted

技术标签:

【中文标题】位置为空融合位置提供者android【英文标题】:Location is null Fused location provider android 【发布时间】:2017-07-01 18:51:32 【问题描述】:

我正在检查移动设备上是否启用了位置服务,如果是,请监听构建 googleapiclient 并监听位置更改。我面临的问题是,一旦在移动设备中关闭位置并重新启用,我就无法获得位置(纬度/经度)。位置始终为null

isLocationEnabled - 检查位置是否已打开

            if (isLocationEnabled(getApplicationContext())) 
                mMenu.getItem(0).setVisible(true);
            
            else 
                Snackbar.make(mLayout, getString(R.string.loc_not_enable),
                        Snackbar.LENGTH_LONG).show();
            
        
    );
    //setup GoogleApiclient
    buildGoogleApiClient();


 public static boolean isLocationEnabled(Context context) 
    int locationMode = 0;
    String locationProviders;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        try 
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
         catch (SettingNotFoundException e) 
            e.printStackTrace();
            return false;
        
        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    else
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    



private synchronized void buildGoogleApiClient() 
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();


@Override
protected void onStart() 
    // Connect the client.
    super.onStart();
    if (mGoogleApiClient != null) 
        mGoogleApiClient.connect();
    


@Override
protected void onStop() 
    // Disconnecting the client invalidates it.
    mGoogleApiClient.disconnect();
    super.onStop();


@Override
public void onConnected(Bundle bundle) 
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) 
        ActivityCompat.requestPermissions(this, new String[]android.Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_LOCATION);
     else 
        startLocationServices();

    


private void startLocationServices() 
    try 
        LocationRequest mLocationRequest = LocationRequest.create();
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation==null)
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
         else 
            Log.d("mLastLocation", String.valueOf(mLastLocation));
            lat = String.valueOf(mLastLocation.getLatitude());
            lon = String.valueOf(mLastLocation.getLongitude());
        
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(10*1000); // Update location every 10 seconds
        mLocationRequest.setFastestInterval(1 * 1000);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
     catch (SecurityException exception) 
        exception.printStackTrace();
    



   E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.my.game.wesport, PID: 30629
              java.lang.NumberFormatException: empty String
                  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                  at java.lang.Double.parseDouble(Double.java:547)
                  at com.my.game.wesport.MapsActivity.onMapReady(MapsActivity.java:161)
                  at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                  at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                  at android.os.Binder.transact(Binder.java:499)
                  at zu.a(:com.google.android.gms.DynamiteModulesB:82)
                  at maps.ad.t$5.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

【问题讨论】:

【参考方案1】:

重启手机后问题已解决。

【讨论】:

以上是关于位置为空融合位置提供者android的主要内容,如果未能解决你的问题,请参考以下文章

重新启动活动时,Android Wear 上的融合位置提供程序会导致崩溃

Android 中的融合位置提供程序

融合的位置提供程序 setsmallestDisplacement 在 Android 中不起作用

融合位置提供程序 - 通过位移更新位置不起作用

融合位置提供程序和“Wi-Fi 和移动网络位置”设置

使用融合位置提供程序获取用户当前位置