2014-01-26 154 views
0

我有两个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. 我希望这是错误的权限设置为可启动的活动为发射器将没有权限推出这款activity.Is正确的吗?

  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. 如果提供的应用程序被安装在前,后跟请求者,则请求者就能启动供应商的活动,但如果它是首先被安装在请求者,这不会发生。如何解决这个问题?

回答

2

我希望这是错误的权限设置为可启动的活动为发射器将没有权限推出这款activity.Is正确的吗?

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

提供程序的清单应该声明提供程序应用程序需要权限 - com.example.permissionprovider.CUSTOM,还是不需要,因为权限是在同一个清单中定义的?

您可以定义在这两个应用的<permission>,对付你的最后一个问题:

如果安装了供应商的应用程序首先随后请求,则请求就可以启动供应商活动,但如果它是首先安装的请求者,则不会发生这种情况。如何解决这个问题?

在两个应用程序中定义<permission>。此外,如果您要发布这两个应用程序,并且没有第三方应该使用您的提供商,请使用signature级许可权,而不是normal-一。这会阻止除你之外的任何人编写可成功保存许可的应用程序。

+0

非常感谢你:)。添加到两个应用程序解决了最后一个问题。问题2我认为我不太清楚。我希望只在请求者而不是在提供者中是必需的,除非请求者有一些被相同权限保护的组件并且提供者试图激活该组件。 – coderplus