從Android 6.0開始,有部分危險權限申請需要在運行時候申請,一般是你的目標版本targetSdkVersion大於或等於23就要動態申請權限了,否則APP在運行當中會崩潰。
在Ubuntu Server 14.04上編譯Android 6.0源碼 http://www.linuxidc.com/Linux/2016-06/132041.htm
Android 6.0百度網盤下載 http://www.linuxidc.com/Linux/2016-01/127285.htm
權限分兩種:正常權限和危險權限。危險權限在targetSdkVersion大於或等於23就要動態申請權限了。危險權限有以下幾種:
每組權限中,用戶只要授權該組下的一個權限,該組中所有權限都可以用。
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)
如果應用之前請求過此權限但用戶拒絕了請求,此方法將返回 true。
如果用戶在過去拒絕了權限請求,並在權限請求系統對話框中選擇了 Don’t ask again 選項,此方法將返回 false。
如果設備規范禁止應用具有該權限,此方法也會返回 false。所以第一次進入APP申請該權限時,有些設備會返回false(第二次申請才為true)。
ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS)
返回PackageManager.PERMISSION_GRANTED表示已擁有,不具有則返回PackageManager.PERMISSION_DENIED
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
ActivityCompat.requestPermissions用於申請權限。
thisActivity,不用多說了,就是當前的Activity名字的一個實例(引用)。
new String[]{Manifest.permission.READ_CONTACTS}是你要申請的權限,你可以寫幾個權限進入數組裡申請。
MY_PERMISSIONS_REQUEST_READ_CONTACTS為你自己定義一個對應的號碼,方便系統回調時識別。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}
這裡處理的是權限相應結果。
requestCode對應之前的你自己定義的號碼MY_PERMISSIONS_REQUEST_READ_CONTACTS。
grantResults是權限申請結果數組。其結果可以是PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Log.d(TAG, "getPackageName(): " + activity.getPackageName());
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
這裡填寫的權限和普通的權限相差無異。
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
下面來實現一個單權限的申請。
package com.example.test;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
private static String TAG = "MainActivity";
View mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.activity_main);
}
@Override
public boolean shouldShowRequestPermissionRationale(String permission) {
return super.shouldShowRequestPermissionRationale(permission);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
openSettingActivity(this, "沒有此權限,無法開啟這個功能,請開啟權限。PERMISSION_READ_ACCOUNTS 和 PERMISSION_GET_ACCOUNTS" );
}
}
private static void openSettingActivity(final Activity activity, String message) {
showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Log.d(TAG, "getPackageName(): " + activity.getPackageName());
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
});
}
private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
public void showContacts(View v) {
Log.i(TAG, "Show contacts button pressed. Checking permissions.");
//檢查是否夠權限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions.");
requestContactsPermissions();
}
}
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
/**
* Id to identify a contacts permission request.
*/
private static final int REQUEST_CONTACTS = 1;
/**
* 申請權限
*/
private void requestContactsPermissions() {
//檢查是否用戶點擊了不再提示選項
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_CONTACTS)) {
Log.i(TAG, "Displaying contacts permission rationale to provide additional context.");
//提示信息
Snackbar.make(mLayout, "Contacts permissions are needed to demonstrate access.", Snackbar.LENGTH_INDEFINITE)
.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
}
})
.show();
} else {
// Contact permissions have not been granted yet. Request them directly.
ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
}
}
}
權限申請清單
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
實現流程:
1.檢查是否有該權限
2.檢查是否解析受限,如果有則直接申請權限,沒有則彈窗提示用戶申請權限。
3.系統回調onRequestPermissionsResult反饋申請結果。
4.如果用戶拒絕就彈窗提示是否跳轉到設置裡面的應用軟件權限設置頁面。
多權限申請和單權限申請類似,在申請時候申請多個而已。
List<string> requestPermissions = new ArrayList<string>();
requestPermissions.add(Manifest.permission.RECORD_AUDIO);
requestPermissions.add(Manifest.permission.GET_ACCOUNTS);
requestPermissions.add(Manifest.permission.READ_PHONE_STATE);
requestPermissions.add(Manifest.permission.CALL_PHONE);
requestPermissions.add(Manifest.permission.CAMERA);
requestPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
requestPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
requestPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
requestPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(MainActivity.this, requestPermissions.toArray(new String[requestPermissions.size()]), REQUEST_CONTACTS);
記得在AndroidManifest裡填寫對應的權限。
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
相關資料鏈接:
https://developer.android.com/training/permissions/requesting.html#perm-request
https://developer.android.com/guide/topics/security/permissions.html#defining
https://github.com/qianxiaoai/RuntimePermissionsDemo/tree/dev
谷歌Demo地址:https://github.com/googlesamples/android-RuntimePermissions
本文Demo到Linux公社資源站下載:
------------------------------------------分割線------------------------------------------
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2017年資料/1月/17日/Android 6.0動態權限申請/
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11