0

ロケーションマネージャからlastknownlocationを取得するコードを作成しています。これにより、ランタイム権限を追加するように求められました。ここで marshmallowでロケーション権限を追加する

が私のコードです:私はまだエラーを取得しています

public class MainActivity extends AppCompatActivity implements LocationListener { 

    LocationManager locationManager; 
    String provider; 
    private final int MY_PERMISSIONS_REQUEST_CODE=1; 
    Location location; 
    Boolean isPermissionGranted=false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

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

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


     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},MY_PERMISSIONS_REQUEST_CODE); 
      } 

      // return; 
     } 

    } 

    public void getlastknownposition() 
    { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 


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


     if(requestCode == MY_PERMISSIONS_REQUEST_CODE) 
     { 
      if(grantResults[0]==PackageManager.PERMISSION_GRANTED && grantResults[1]==PackageManager.PERMISSION_GRANTED){ 

      Toast.makeText(MainActivity.this," granted "+grantResults[0]+"granted2"+grantResults[1], Toast.LENGTH_SHORT).show(); 


      location = locationManager.getLastKnownLocation(provider);} 

     } 

    } 

} 

「コールは、ユーザによって拒否することができる権限が必要です。コードが明示的に許可が利用可能な場合(checkPermissionで)を確認したり、明示的に処理する必要がありますonPermissionsResult =であればラインlocation = locationManager.getLastKnownLocation(provider);

+0

location = locationManager.getLastKnownLocation(provider); 

を交換しては、あなたがその行をラップしようとした持ち '(ActivityCompat.checkSelfPermission' ..私は、エラーメッセージがかなり明示であると思われる場合 - ちょうどあなたのために'onRequestPermissionResult'の中から呼び出しを行っていても、チェックする必要はありません。また、' try/catch'でその行をラップすることもできます。エラーは明示的です。潜在的な ' SecurityException'。 – trooper

+0

おそらく 'requestPermissi ons'から 'ActivityCompat.requestPermissions'に変更します。 – kimbaudi

+0

これを試してみてくださいhttp://stackoverflow.com/a/41221852/5488468 –

答えて

0

onRequestPermissionsResultの潜在的SecurityException は、第二の周りにいくつかのブレースを入れて)

if(requestCode == MY_PERMISSIONS_REQUEST_CODE){ 
     if(grantResults[0]==PackageManager.PERMISSION_GRANTED && grantResults[1]==PackageManager.PERMISSION_GRANTED){ 
      Toast.makeText(MainActivity.this," granted "+grantResults[0]+"granted2"+grantResults[1], Toast.LENGTH_SHORT).show(); 

      location = locationManager.getLastKnownLocation(provider); 
     } else { 
       //TODO handle user saying NO! :) 
     } 
    } 

ヒント:FINE_LOCATIONを要求した場合は、COARSEは必要ありません。すでに含まれています。

+0

まだ同じエラーがあります。 –

+0

コメントの中で示唆するようにtry/catchで囲んで、私が持っていたプロジェクトをチェックしました。実際にはメソッドを呼び出していて、そのメソッドはセキュリティ例外をスローできます。他の回避策は考えられません。 – Alqueraf

0

ライン

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
{ 
    location = locationManager.getLastKnownLocation(provider); 
} 
関連する問題