Android 6.0的权限不再是在安装的时候一股脑给你了。需要权限的话,需要代码中去获取。而对于用户,获取权限会弹框,用户可以醒目地知道应用拿了什么权限,用户进而可以决定给不给这个权限。
例如下面的例子是一个垃圾程序要获取短信的权限
在代码只需要加一句就可以了,参数里面了的2我是随便乱写的,就是要输入一个整形,代表request code。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.requestPermissions(
new String[]{Manifest.permission.SEND_SMS},
2);
}
这篇文章不说太多,就说下这个弹框是怎么出来的好了。
首先入口是Activity类,调用requestPermissions获取权限。可以看到他启动了另一个Activity。可以想象,这个弹框就是在这个Activity里面。我们看看这个Intent是什么,找出Intent的包名与Action。
/frameworks/base/core/java/android/app/Activity.java
public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
}
通过ContextImpl.getPackageManager()获取到的是ApplicationPackageManager,他的父类是PackageManager。
/frameworks/base/core/java/android/app/ContextImpl.java
@Override
public PackageManager getPackageManager() {
if (mPackageManager != null) {
return mPackageManager;
}
IPackageManager pm = ActivityThread.getPackageManager();
if (pm != null) {
// Doesn't matter if we make more than one instance.
return (mPackageManager = new ApplicationPackageManager(this, pm));
}
return null;
}
ApplicationPackageManager获取到这个intent的action是android.content.pm.action.REQUEST_PERMISSIONS,包名要再看看,是通过getPermissionControllerPackageName()获取的。
/frameworks/base/core/java/android/app/ApplicationPackageManager.java
public static final String ACTION_REQUEST_PERMISSIONS =
"android.content.pm.action.REQUEST_PERMISSIONS";
public static final String EXTRA_REQUEST_PERMISSIONS_NAMES =
"android.content.pm.extra.REQUEST_PERMISSIONS_NAMES";
public Intent buildRequestPermissionsIntent(@NonNull String[] permissions) {
if (ArrayUtils.isEmpty(permissions)) {
throw new NullPointerException("permission cannot be null or empty");
}
Intent intent = new Intent(ACTION_REQUEST_PERMISSIONS);
intent.putExtra(EXTRA_REQUEST_PERMISSIONS_NAMES, permissions);
intent.setPackage(getPermissionControllerPackageName());
return intent;
}
getPermissionControllerPackageName()会进一步调用mPM.getPermissionControllerPackageName(),这个mPM是系统服务PackageManagerService的远程代理。
@Override
public String getPermissionControllerPackageName() {
synchronized (mLock) {
if (mPermissionsControllerPackageName == null) {
try {
mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
} catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
}
return mPermissionsControllerPackageName;
}
}
最后包名通过PackageManagerService的getRequiredInstallerLPr获取到,看这个函数名,可以顾名思义下,就是安装器,就是安装apk那个玩意。
/ frameworks/ base/ services/ core/ java/ com/ android/ server/ pm/ PackageManagerService.java
private String getRequiredInstallerLPr() {
Intent installerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installerIntent.addCategory(Intent.CATEGORY_DEFAULT);
installerIntent.setDataAndType(Uri.fromFile(new File("foo.apk")), PACKAGE_MIME_TYPE);
final List installers = queryIntentActivities(installerIntent,
PACKAGE_MIME_TYPE, 0, 0);
String requiredInstaller = null;
final int N = installers.size();
for (int i = 0; i < N; i++) {
final ResolveInfo info = installers.get(i);
final String packageName = info.activityInfo.packageName;
if (!info.activityInfo.applicationInfo.isSystemApp()) {
continue;
}
if (requiredInstaller != null) {
throw new RuntimeException("There must be one required installer");
}
requiredInstaller = packageName;
}
if (requiredInstaller == null) {
throw new RuntimeException("There must be one required installer");
}
return requiredInstaller;
}
所以,最后的组合而成的intent的包名是com.android.packageinstaller,action是android.content.pm.action.REQUEST_PERMISSIONS。
这个Action会启动应用安装器的GrantPermissionsActivity 进行授权
所以啦,这个弹框是由GrantPermissionsActivity中弹出的。分析完毕!