2017-02-04 96 views
2

我是android編程的新手,我需要顯示警報,以便用戶在點擊允許按鈕時自動授予我的應用程序權限。顯示允許應用程序訪問設備的位置

喜歡這個程序

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

喜它是一個運行時間許可,只是去通過這個環節做參考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(第二張截圖)不同。你實際上需要兩者:首先檢查你是否有其他答案建議的權限,然後檢查GPS是否已啓用,因爲你已經在做。

如果要以編程方式啓用GPS,則需要使用「設置API」。檢查this answer如何實施它。

相關問題