1. 程式人生 > >Android:java.lang.SecurityException: Permission Denial解決辦法

Android:java.lang.SecurityException: Permission Denial解決辦法

有時程式需要我們建立自己的內容提供器,可能出現java.lang.SecurityException: Permission Denial: opening provider com.example.db.PersonProvider from ProcessRecord錯誤訊息。

這是因為在你的提供外部訪問介面的應用程式中沒有提供給其他程式訪問資料的許可權。

解決方法:
在你的provider標籤裡新增android:exported屬性

<provider                  android:name="com.example.databasetest.DatabaseProvider"
android:authorities="com.example.databasetest.provider" android:exported="true" >
</provider>

android文件裡的內容

android:exported
Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application can use the provider’s content URI toaccess it, subject to the permissions specified for the provider.
false

: The provider is not available to other applications. Set
android:exported=”false” to limit access to the provider to your
applications. Only applications that have the same user ID (UID) as the provider will have access to it.
The default value is “true” for applications that set either android:minSdkVersion or android:targetSdkVersion to “16” or lower. For applications that set either of these attributes to “17” or higher, the default is “false”.

You can set android:exported=”false” and still limit access to your
provider by setting permissions with the permission attribute.