我创建了一个自定义内容提供程序,将由更多应用程序访问.我在我的提供程序AndroidManifest.xml文件中包含了权限TAG,在第二个应用程序中,我包含了uses-permissions标记,但没有成功. Logcat告诉我:
java.lang.SecurityException: Permission Denial: opening provIDer com.company.contentprovIDer.AplicacaoContentProvIDer requires READ_DATABASE or WRITE_DATABASE.
我搜索过类似的问题,但似乎一切都是正确的.有任何想法吗 ?
谢谢 !!!
这是我的提供者AndroIDManifest.xml文件:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="com.company.contentprovIDer"androID:versionCode="1"androID:versionname="1.0" ><uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="17" /><permission androID:name="READ_DATABASE" androID:label="@string/app_read" androID:protectionLevel="normal"></permission><permission androID:name="WRITE_DATABASE" androID:label="@string/app_write" androID:protectionLevel="normal"></permission><application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name=".CompanyProvIDer" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <provIDer androID:name="AplicacaoContentProvIDer" androID:authoritIEs="com.company.contentprovIDer" androID:exported="true" androID:readPermission="@string/app_read" androID:writePermission="@string/app_write" /></application>
这是我的第二个应用程序AndroIDManifest.xml文件:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="com.example.testeprovIDer"androID:versionCode="1"androID:versionname="1.0" ><uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="16" /><uses-permission androID:name="androID.permissions.READ_DATABASE"/><uses-permission androID:name="androID.permissioms.WRITE_DATABASE"/><application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="com.example.testeprovIDer.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>
解决方法:
上面的答案让我感到困惑.但我现在明白了.我也想发布我的解决方案.也许对于某人来说,理解它会更好.
First App A是具有sqlite-Database和“Custom Content ProvIDer”的应用程序. App B使用ContentResolver来自App A的数据库.
这是来自App A的AndroIDManifest.xml文件:
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="de.test"androID:versionCode="1"androID:versionname="1.0" ><uses-sdk androID:minSdkVersion="10" /><permission androID:name="de.test.READ_DATABASE" androID:protectionLevel="normal" /><permission androID:name="de.test.WRITE_DATABASE" androID:protectionLevel="normal" /><application androID:deBUGgable="true" ... > ... ... <provIDer androID:name="de.test.TestContentProvIDer" androID:authoritIEs="de.test.ContentProvIDer" androID:exported="true" androID:readPermission="de.test.READ_DATABASE" androID:writePermission="de.test.WRITE_DATABASE" /> ... ...</application>
好的,这是来自App B的AndroIDManifest.xml文件.重要的是具有“uses-permission”的部分:
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="de.test.testercontentprovIDer"androID:versionCode="1"androID:versionname="1.0" ><uses-sdk androID:minSdkVersion="15" androID:targetSdkVersion="17" /><uses-permission androID:name="de.test.READ_DATABASE" /><uses-permission androID:name="de.test.WRITE_DATABASE" /><application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="de.test.testercontentprovIDer.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>
App A的ContentProvIDer代码如下所示:
public class TestContentProvIDer extends ContentProvIDer {public static final String AUTHORITY = "de.test.TestContentProvIDer";public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + "nameoftable");@OverrIDepublic boolean onCreate() { ... return true;}@OverrIDepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Todo auto-generated method stub return null;}@OverrIDepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // Todo auto-generated method stub return 0;}@OverrIDepublic int delete(Uri uri, String selection, String[] selectionArgs) { // Todo auto-generated method stub return 0;}@OverrIDepublic Uri insert(Uri uri, ContentValues values) { // Todo auto-generated method stub return null;}@OverrIDepublic String getType(Uri uri) { // Todo auto-generated method stub return null;}}
App B中的ContentResolver代码:
public class MainActivity extends Activity {private static final String TAG = MainActivity.class.getSimplename();public static final String AUTHORITY = "de.test.TestContentProvIDer";public static final String table_name = "nameoftable"; ...@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ContentResolver cr = getContentResolver(); // show entrIEs of db ListEntrIEs(cr);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}private voID ListEntrIEs(ContentResolver cr) { Uri uri = Uri.parse("content://" + AUTHORITY + "/" + table_name); Cursor c = cr.query(uri, null, null, null, null); if (c == null) { Log.d(TAG, "Cursor c == null."); return; } while (c.movetoNext()) { String column1 = c.getString(0); String column2 = c.getString(1); String column3 = c.getString(2); Log.d(TAG, "column1=" + column1 + " column2=" + column2 + " column3=" + column3); } c.close();}}
我希望这可以帮助别人更好地理解它.
总结以上是内存溢出为你收集整理的android – 权限拒绝:开放提供者全部内容,希望文章能够帮你解决android – 权限拒绝:开放提供者所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)