Android权限 - 最佳实践



我有两个Android应用程序,一个许可提供商和一个请求者。

提供商具有以下清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.permissionprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<permission android:description="@string/permission_desc"
        android:icon="@drawable/ic_launcher"
        android:label="some permission"
        android:name="com.example.permissionprovider.CUSTOM"
        android:protectionLevel="normal" />
<!-- is the below tag required in the provider?--> 
<uses-permission android:name="com.example.permissionprovider.CUSTOM"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.permissionprovider.MainActivity"
        android:permission="com.example.permissionprovider.CUSTOM"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>
  1. 我希望将许可设置为可发射活动是错误的,因为发射器没有权限启动此活动。这是正确的吗?

  2. 提供商的清单是否应声明提供者应用程序需要许可-com.example.permissionProvider.custom,还是不需要,因为在同一清单中定义了权限?

现在我有一个请求者应用,该应用具有以下清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.permissionrequester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="com.example.permissionprovider.CUSTOM"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.permissionrequester.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

,请求者试图启动提供商的活动。

Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("com.example.permissionprovider");
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
    }    }
  1. 如果首先安装了提供商应用程序,则请求者将能够启动提供商的活动,但是如果首先安装的请求者,这不会发生。如何解决这个问题?

我希望将权限设置为可发射活动是错误的,因为启动器没有权限启动此活动。

是正确的吗?

正确,启动器将无法启动该活动。但是,它仍然会出现在启动器中,因此,当用户无法启动您的应用时,用户会感到沮丧。

提供商的清单是否应声明提供商应用程序需要许可-com.example.permissionprovider.custom,还是在同一清单中定义许可时不需要它?

您可以在两个应用程序中定义<permission>,以处理您的最后一个问题:

如果首先安装了提供商应用程序,则随后是请求者,则请求者将能够启动提供商的活动,但是如果首先安装的请求者,这不会发生。如何解决这个问题?

在两个应用程序中定义<permission>

另外,如果您要发布两个应用程序,并且没有第三方不应使用您的提供商,请使用signature级别的权限,而不是normal -ONE。这将阻止您以外的任何人编写可以成功持有许可的应用程序。

最新更新