Android 从不同的应用程序访问自定义内容提供商
您好,我已经创建了一个android应用程序,它使用一个名为CustomCP的自定义内容提供商, 它实现了所有的方法,在管理应用程序内部的数据时,一切都很好, 但是,当我试图从另一个应用程序访问它时,我总是得到一个错误“找不到” com.example.customcp的提供商信息 我已在第一个应用程序的清单文件中将我的内容提供商声明为Android 从不同的应用程序访问自定义内容提供商,android,android-contentprovider,Android,Android Contentprovider,您好,我已经创建了一个android应用程序,它使用一个名为CustomCP的自定义内容提供商, 它实现了所有的方法,在管理应用程序内部的数据时,一切都很好, 但是,当我试图从另一个应用程序访问它时,我总是得到一个错误“找不到” com.example.customcp的提供商信息 我已在第一个应用程序的清单文件中将我的内容提供商声明为 <provider android:name="com.example.CustomCP" android:authorities="com.e
<provider android:name="com.example.CustomCP" android:authorities="com.example.customcp"/>
因此,问题很简单,是否可以从多个应用程序访问自定义内容提供商?在清单文件中,确保“提供商android..>”位于 "provider android..>" "application .. /application>" “申请……”/应用程序>”
希望这有帮助是的,可以从另一个应用程序访问自定义内容提供商。使用您的术语,我们将内容提供商称为CustomCP,另一个应用程序称为AppA。(AppA是希望访问提供商的应用程序)。此方法已被证明有效:
uriyourURI=Uri.parse(“content://com.example.customcp/YourDatabase");
ContentProviderClient yourCR=getContentResolver().acquireContentProviderClient(yourURI);
yourCursor=yourCR.query(yourcuri,null,null,null);
注意:您必须将代码包含在try/catch块中,或者包含“throws RemoteException”,因为提供程序不在应用程序a中<provider
android:name="your.package.contentprovider.YourProvider"
android:authorities="YourAuthority"
android:readPermission="android.permission.permRead"
android:exported="true" >
</provider>
创建内容提供程序后,请在清单文件中指定内容提供程序。您可以使用标记提及内容提供程序。在提供程序标记内,不要忘记提及名称和权限属性。此声明应
<provider
android:name="pakgName.ProviderClassName"
android:authorities="pakgName.ProviderClassName"
android:multiprocess="true" >
</provider>
这里是您在Authories属性中提到的内容,当您尝试从提供商处获取数据时,这些属性应该是匹配的。我知道我迟到了,但为了防止有人偶然发现这个问题,我认为这是一个。这一个救了我一天。因此,在这种情况下,我们在一个应用中使用了CustomCP。例如,另一个应用称为AppDemo2。然后首先定义CustomCPs AndroidManifest.xml中的权限:
<permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>
<application ...
...></application>
接下来,您必须创建一个提供程序:
<application ... >
<provider
android:authorities="com.package_name_where_customCP_is"
android:name=".CustomCP"
android:readPermission="com.any_string.MY_PERMISSION_FOR_SOMETHING"
android:enabled="true"
android:exported="true"
android:multiprocess="true"
>
</provider>
<activity...>
最后,在您的第二个应用程序(我的答案中的AppDemo2)中,打开其清单文件并添加此权限:
<uses-permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>
这是对我有用的答案。我希望这有帮助:)您可以使用以下代码在其他应用程序中获取内容提供商数据。。 您需要使用内容解析器来获取数据
try {
//URL provided in first app while creating the content provider
val URL = "content://com.test.localstorage.MyContentProvider/authentication"
val data = Uri.parse(URL)
val cursor: Cursor? = contentResolver.query(data, null, null, null, "user_id")
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
return cursor.getString(cursor.getColumnIndex("user_id")).toString()
} while (cursor.moveToNext())
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
我知道您基本上缺少
ContentResolver
来访问另一个应用程序的ContentProvider
。您需要使用ContentResolver
而不是managedQuery
我尝试了Uri kUri=Uri.parse(“content://com.example.customcp/key");ContentProviderClient cr=getContentResolver().acquireContentProviderClient(kUri);尝试{Cursor c=cr.query(kUri,null,null,null);}catch(RemoteException e){//TODO自动生成的catch块e.printStackTrace();},但仍然在logcat上遇到相同的错误“找不到提供程序”“同样,在编写ContentProvider
的第一个应用程序中,您需要在该应用程序的清单文件中声明它。你这么做了吗?是的,如果你是指这部分:'',它在第一份清单上。@PeteH,你的答案是好的(+1)。但我不明白android:exported=“true”在第四步中的意义。为什么这里需要android:multiprocess=“true”?据我所知,默认情况下导出内容提供商。所以你不必在舱单上声明。另外,如果只需要使用CP的读取权限,则必须从访问者应用程序端授予它。
try {
//URL provided in first app while creating the content provider
val URL = "content://com.test.localstorage.MyContentProvider/authentication"
val data = Uri.parse(URL)
val cursor: Cursor? = contentResolver.query(data, null, null, null, "user_id")
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
return cursor.getString(cursor.getColumnIndex("user_id")).toString()
} while (cursor.moveToNext())
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}