我想知道是否有當用戶按下「允許」的聯繫方式按鈕進入/日曆訪問等等。我們可以找出事件的方式,Android權限:用戶按下後執行的任務「允許」
我知道有一種方法,要求與ActivityCompat.requestPermissions權限,但在那裏執行用戶授予權限之後的行動呢?
我想知道是否有當用戶按下「允許」的聯繫方式按鈕進入/日曆訪問等等。我們可以找出事件的方式,Android權限:用戶按下後執行的任務「允許」
我知道有一種方法,要求與ActivityCompat.requestPermissions權限,但在那裏執行用戶授予權限之後的行動呢?
首先定義變量:
public static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
請求許可使用:
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
現在使用捕捉結果:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION : {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
FOR FRAGMENT
如果您在fragment
嘗試此代碼,更改
checkSelfPermission()
到
ActivityCompact.checkSelfPermission()
,改變
ActivityCompat.requestPermissions()
到
requestPermissions()
許可結果的處理(允許或拒絕)是相同的活性。
更完整的例子來看看這個Answer Here
謝謝@ rafsanahmad007這就是我一直在尋找 –
根本 –
卸載工作,乾淨生成APK ......請務必在清單中添加的權限也...權限對話框不會顯示Android版本低於marsmallow。 – rafsanahmad007
呼叫requestPermissions()
請求的權限。您可以在onRequestPermissionsResult()
中找到結果,並確定他們是否授予了許可。從應用程序代碼的角度來看,這就是「之後」。
我用這個代碼,這個根本目的。
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA
}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
return true;
}
}
然後,您可以撥打:
if(isPermissionGranted())
{
// do your stuff
}
它不斷崩潰我的應用程序 –
是。回調函數'onRequestPermissionsResult(..)'返回的提示的結果可用於執行任何進一步的代碼。 – Pztar