2017-02-09 24 views
-2

Я столкнулся ошибку в разрешении местоположения с андроидаошибка Android Разрешение в requestLocationUpdates

и это в на OnCreate

onCreate(Bundle savedInstanceState){ 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    provider = locationManager.getBestProvider(new Criteria(), false); 
    } 

этого onResume

protected void onResume() { 
    super.onResume(); 


    try { 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      getLocationPermission(); 
     } 
     if (permissionGranted) { 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 

     } 
    }catch (Exception e) { 

     e.printStackTrace(); 


    } 

и это функции getLocationPermission()

private void getLocationPermission() { 
    /* 
    * Request location permission, so that we can get the location of the 
    * device. The result of the permission request is handled by a callback, 
    * onRequestPermissionsResult. 
    */ 
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     permissionGranted = true; 
    } else { 
     ActivityCompat.requestPermissions(this, 
       new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
       MY_PERMISSION_ACCESS_FINE_LOCATION); 

    } 
    /* 
    * Get the best and most recent location of the device, which may be null in rare 
    * cases when a location is not available. 
    * Also request regular updates about the device location. 
    */ 
    if (permissionGranted) { 

     Log.i("Permission",String.valueOf(permissionGranted)); 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 

     Log.i("Location", location.toString()); 
    } 
} 

и это onRequestPermissionsResult

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     for (int result : grantResults) { 
      if (result == PackageManager.PERMISSION_DENIED) { 
       // User Refused to give permission 
       Toast.makeText(getApplicationContext(), "Please We Need Have Your Current Location", Toast.LENGTH_SHORT).show(); 
      } else if (result == PackageManager.PERMISSION_GRANTED) { 
       permissionGranted = true; 
      } 
     } 
    } 

и это ошибка после разрешения местоположения принял

W/System.err: java.lang.IllegalArgumentException: invalid provider: null 
W/System.err:  at android.location.LocationManager.checkProvider(LocationManager.java:1704) 
W/System.err:  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459) 
W/System.err:  at com.osamaa.appdevstart1.MapsActivity.onResume(MapsActivity.java:322) 
W/System.err:  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) 
W/System.err:  at android.app.Activity.performResume(Activity.java:6312) 
W/System.err:  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) 
W/System.err:  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
W/System.err:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
W/System.err:  at android.os.Looper.loop(Looper.java:148) 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Я думаю, что проблема здесь связана с разрешения и способ заказа locationManger

+0

менеджер @arjun_sna расположение Android ядра. Здесь пусто. Вместо публикации кода LocationManager вы можете объяснить, где вы запускаете 'провайдер'? – TruongHieu

+0

поставщик не подключен, запрос вызоваLocationUpdates после провайдера onConnected. – Gautam

+0

Я добавляю это onCreate() locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); provider = locationManager.getBestProvider (новые критерии(), false); –

ответ

0

использовать это

GoogleApiClient  mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(INTERVAL); 
      mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER); 

      mGooglePlaceApiClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(connectionPlaceListener) 
        .addOnConnectionFailedListener(connectionFailedListener) 
        .build(); 
      mGooglePlaceApiClient.connect(); 

соединения Callbacks

private GoogleApiClient.ConnectionCallbacks connectionPlaceListener = new GoogleApiClient.ConnectionCallbacks() { 
     @Override 
     public void onConnected(Bundle bundle) { 
      if (CommonCode.checkAndroidVerSionFor23()) { 
       // Marshmallow+ 
       if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) 
         != PackageManager.PERMISSION_GRANTED) { 
        return; 
       } 
      } 

       LocationServices.FusedLocationApi.requestLocationUpdates(mGooglePlaceApiClient, mLocationRequest, ScreenService.this); 
       CommonCode.printLoge(TAG, "Location update started ..............: "); 

     } 

     @Override 
     public void onConnectionSuspended(int i) { 
      CommonCode.printLoge("Place service", "Connecting to GoogleApiClient suspended."); 
     } 
    }; 

 Смежные вопросы

  • Нет связанных вопросов^_^