在Android 6.0中引入的新的运行权限模型( runtime permission model),是一个值得Android开发者考虑的重要问题。在Android 6.0之前,作为一个开发者,你可能会在AndroidManifest.xml文件中定义所需的权限,并专注于您的业务逻辑。然而,由于Android 6.0的出现,故事变得更复杂了,它为用户提供了更多的安全性和可控性。让我们使用一个简单的例子来解决这个问题。
假设,我想读取存储在手机中的所有联系人的名字,我该怎么办?
步骤1:
创建一个名为“Contacts Reader”的新项目,包名“com.javahelps.contactsreader”。
第2步:
我们需要在AndroidManifest.xml文件中定义的许可,所以在AndroidManifest.xml请求给定权限。
添加权限后,AndroidManifest.xml文件应该是这样的:
package="com.javahelps.contactsreader">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
第3步:
添加一个ListView到activity_main.xml布局中。
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
android:id="@+id/lstNames"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
步骤4:
在MainActivity.java创建ListView控件的实例变量,并在onCreate方法中找到这个对象。
public class MainActivity extends AppCompatActivity {
// The ListView
private ListView lstNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the list view
this.lstNames = (ListView) findViewById(R.id.lstNames);
// Read and show the contacts
showContacts();
}
}
第5步:
由于这个例子的目的不是解释“怎么读取联系人”,只需添加下面的方法到MainActivity.java。
/**
* Read the name of all the contacts.
*
* @return a list of names.
*/
private List getContactNames() {
List contacts = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return contacts;
}
这个方法将读取所有的联系人的姓名,并作为一个List返回。
步骤6:
添加另一个方法showContacts(),这将使用上述方法来取得联系人的列表,并在ListView中显示。我们将从onCreate()中调用这个方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the list view
this.lstNames = (ListView) findViewById(R.id.lstNames);
// Read and show the contacts
showContacts();
}
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
List contacts = getContactNames();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contacts);
lstNames.setAdapter(adapter);
}
昨晚所有这些改变后,MainActivity.java应该是这样的:
package com.javahelps.contactsreader;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// The ListView
private ListView lstNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the list view
this.lstNames = (ListView) findViewById(R.id.lstNames);
// Read and show the contacts
showContacts();
}
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
List contacts = getContactNames();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contacts);
lstNames.setAdapter(adapter);
}
/**
* Read the name of all the contacts.
*
* @return a list of names.
*/
private List getContactNames() {
List contacts = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return contacts;
}
}
步骤7:
在Android 5.0版或更小版本的模拟器或Android设备上运行该应用程序。该程序应该没有任何问题,并列出可用的联系人姓名。
Step 8: Run the same application in an emulator or Android device with Android version 6.0 or higher and check the output. You will get an exception like this:
中文(简体)
步骤8:
在Android 6.0或更高版本的模拟器或Android设备上运行同一应用程序,并检查输出。 你会得到这样一个异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javahelps.contactsreader/com.javahelps.contactsreader.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{de57b1b 2254:com.javahelps.contactsreader/u0a54} (pid=2254, uid=10054) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
android.app.ActivityThread.-wrap11(ActivityThread.java)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:148)
android.app.ActivityThread.main(ActivityThread.java:5417)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{de57b1b 2254:com.javahelps.contactsreader/u0a54} (pid=2254, uid=10054) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
android.os.Parcel.readException(Parcel.java:1599)
android.os.Parcel.readException(Parcel.java:1552)
android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
android.content.ContentResolver.query(ContentResolver.java:475)
android.content.ContentResolver.query(ContentResolver.java:434)
com.javahelps.contactsreader.MainActivity.getContactNames(MainActivity.java:51)
com.javahelps.contactsreader.MainActivity.showContacts(MainActivity.java:36)
com.javahelps.contactsreader.MainActivity.onCreate(MainActivity.java:29)
android.app.Activity.performCreate(Activity.java:6237)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
android.app.ActivityThread.-wrap11(ActivityThread.java)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:148)
android.app.ActivityThread.main(ActivityThread.java:5417)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
尽管我们已经在清单文件中定义了所需的权限,Android系统却对我们说权限被拒绝了。事实上,这是一个所有Andr??oid开发者都必须面临的问题——运行权限模型。
在Android的6.0中,权限分为普通权限和危险权限。Android开发者指南(Android Developers guide)中定义:
普通权限涵盖应用需要访问应用程序沙箱之外的数据或资源,但侵犯用户的隐私或者操控其他应用程序的风险非常小。比如,允许打开手电筒是一个普通权限。如果一个应用程序声明它需要一个普通权限,系统会自动授予相应权限给这个应用程序。
危险权限涵盖应用程序涉及到的用户隐私,或可能影响用户存储的数据或其他应用程序的运行数据或资源。例如,读取用户联系人是一个危险权限。如果应用声明,它需要危险权限,必须由用户明确地授予应用程序这个权限。
由于READ_CONTACTS是一个危险权限,我们需要请求用户在运行时授予它。让我们来看看,如何要求用户明确授予权限给我们的应用程序。
步骤9:
添加一个新的实例变量PERMISSIONS_REQUEST_READ_CONTACTS,并且像下面给出的一样修改showContacts方法:
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
List contacts = getContactNames();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contacts);
lstNames.setAdapter(adapter);
}
}
现在,showContacts()方法检查SDK版本;如果它比Android 6.0更大,并且如果READ_CONTACTS权限不被许可,便向用户请求READ_CONTACTS权限。checkSelfPermission方法用于确定是否已被授予特定权限。如果版本低于Android的6.0,或者如果权限已被许可,那么可以继续读取联系人。
步骤10:
在权限尚未许可的情况下,Android将请求用户给予相应权限。请求的结果将通过MainActivity中的onRequestPermissionsResult方法传递。在这个方法中,你需要检查用户是否授予了权限,基于这的结果让你的应用程序做出相应的改变。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
showContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}
In this code, if the permission is granted we continue to display the contacts. If not, we simply show a warning message using Toast. After all these modification, the MainActivit.java should look like this:
中文(简体)
在这段代码中,如果权限授予了,我们就继续显示联系人。如果没有,我们只是用Toast显示警告了一条消息。 昨晚所有这些修改后,MainActivit.java应该是这样的:
package com.javahelps.contactsreader;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// The ListView
private ListView lstNames;
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the list view
this.lstNames = (ListView) findViewById(R.id.lstNames);
// Read and show the contacts
showContacts();
}
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
List contacts = getContactNames();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contacts);
lstNames.setAdapter(adapter);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
showContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Read the name of all the contacts.
*
* @return a list of names.
*/
private List getContactNames() {
List contacts = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return contacts;
}
}
步骤11:
保存所有更改,并再次运行应用程序。
还要记住一点,最终用户可以在设置随时更改授予的权限。
确实,运行权限模型是为用户带来了极大的好处,但开发者不得不花一些时间升级他们现有的应用来支持这种模式。如果没有,你的应用程序将无法在Android 6或最新版本上运行。