2017-02-04 4 views
2

私はAndroidプログラミングの初心者です。許可ボタンをクリックしたときに自動的にアプリケーションに許可を与えるように警告を表示する必要があります。このアプリAllow appがデバイスの場所にアクセスするのを表示する

enter image description here

同様

が、実際に私のコードは、ちょうど彼/彼女はそれを手動で変更できるように設定にユーザーを取る、と誰もがそれを手動で行う方法を知ることができませんメッセージを行います

enter image description here

は、どのように私は、最初の警告を得ることができるように、それだけで設定ページにユーザーを移動すると、彼らは肝炎自動的代わりに私の現在の道のアプリゲイン許可ここでは、手動で

それを行うためのeは私のコード

public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity); 
     alertDialog.setTitle("GPS Settings"); 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?"); 

     alertDialog.setPositiveButton("Settings", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(
           Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
         _activity.startActivity(intent); 
        } 
       }); 

     alertDialog.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     alertDialog.show(); 
    } 
+0

最も簡単な解決策http://stackoverflow.com/a/40462527/1677824 – Akhil

答えて

1

あなたはここに

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.READ_CONTACTS) 
    != PackageManager.PERMISSION_GRANTED) { 

// Should we show an explanation? 
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_CONTACTS)) { 

    // Show an explanation to the user *asynchronously* -- don't block 
    // this thread waiting for the user's response! After the user 
    // sees the explanation, try again to request the permission. 

} else { 

    // No explanation needed, we can request the permission. 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
} 
} 

以下のように、ユーザーからの許可を要求する必要があり、参照はlink

1

ですこんにちはそのは、実行時のアクセス許可は、ちょうどrefrence click here

のために、このリンクを経由し、ユーザーを保護するために、このclick here

をチェック実行時に権限を与えなければならないため、ユーザは自分の行動に関連するかどうかを知ることができます。これを行うには

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 

それは彼が場所を使用するようにアプリをautorizeかない天気をユーザーが選択されますするダイアログボックスが表示されます。

次に、この機能を使用してユーザー答えを得る:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case 1: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      } else { 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
     return; 
     } 
      // other 'case' lines to check for other 
      // permissions this app might request 
    } 
} 

ユーザーが一度それを受け入れる場合は、あなたのアプリがそれを覚えているでしょうし、あなたはもうこのダイアログボックスを送信する必要はありません。彼が決めた場合、後でユーザーを無効にすることができます。場所を要求する前に、あなたは許可がまだ付与されているかどうかをテストする必要があります次に:

public boolean checkLocationPermission() 
{ 
    String permission = "android.permission.ACCESS_FINE_LOCATION"; 
    int res = this.checkCallingOrSelfPermission(permission); 
    return (res == PackageManager.PERMISSION_GRANTED); 
} 
+0

リンクが壊れてしまえば、あなたの答えは役に立たない! –

1

ですアプリのデバイスの場所へのアクセス(最初のスクリーンショット)を許可することは、GPSを有効にする(2番目のスクリーンショット)と同じではありません。あなたは実際には両方とも必要です:まず、他の回答が示唆しているように許可を得ているかどうかをチェックして、すでに行っているようにGPSが有効になっているかどうかを確認します。

プログラムでGPSを有効にする場合は、「設定API」を使用する必要があります。実装方法については、this answerを参照してください。

関連する問題