访问不同的应用程序的自定义内容供应商 [英] Accessing custom content provider from different app

查看:120
本文介绍了访问不同的应用程序的自定义内容供应商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我已经创建了一个Android应用程序,使用一个名为CustomCP自定义内容供应商, 它实现了所有的方法,一切工作正常,而在应用程序中管理数据, 但是当我尝试从另一个应用程序,我不断收到一个错误访问它无法找到 对于com.example.customcp供应商信息。

Hello i have created an android app that uses a custom content provider named CustomCP, it implements all methods and everything works fine while managing data inside the app, but when i try to access it from another app i keep getting an error of " Failed to find provider info for com.example.customcp.

我已经宣布了第一个应用程序的清单文件我的内容提供商为

I have declared my content provider in the manifest file of the first app as

<provider android:name="com.example.CustomCP"      android:authorities="com.example.customcp"/>

我尝试调用第二的应用程序的供应商启动活动

I try to call the provider in the second's application start up activity

public class app2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri kUri = Uri.parse("content://com.example.customcp/key");
        Cursor c = managedQuery(kUri, null, null, null, null);
}
}

所以,问题很简单,就是它可以从多个应用程序访问自定义的内容提供商?

So the question is simple , is it possible to access a custom content provider from multiple applications?

推荐答案

是的,这可以从另一个应用程序访问自定义的内容提供商。使用您的术语,我们会打电话给内容提供商CustomCP和其他应用程序APPA。 (APPA是一个想要访问的提供者)。这种方法被证明工作:

Yes, it's possible to access a custom content provider from another app. Using your terminology we'll call the content provider CustomCP and the other app AppA. (AppA is the one that wants to access to the provider). This approach is proven to work:

  1. 从内部APPA通过使用ContentProviderClient指定所需的内容提供商(CustomCP):

  1. Specify the desired content provider (CustomCP) from within AppA by using a ContentProviderClient:

乌里yourURI = Uri.parse(内容://com.example.customcp/YourDatabase); ContentProviderClient yourCR = getContentResolver()acquireContentProviderClient(yourURI);

Uri yourURI = Uri.parse("content://com.example.customcp/YourDatabase"); ContentProviderClient yourCR = getContentResolver().acquireContentProviderClient(yourURI);

访问内容提供商,你通常会从应用程序A.例如:

Access the content provider as you would normally from App A. For example:

yourCursor = yourCR.query(yourURI,NULL,NULL,NULL,NULL);

请注意:您必须附上code一个try / catch块中或包含将抛出RemoteException,因为供应商是不是在应用程序中的

Note: you must either enclose the code within a try/catch block or include a "throws RemoteException" since the provider is not in App A.

CustomCP的清单必须指定供应商,包括允许(例如,读取和/或写)的权限,而提供者必须出口。这里有一个例子:

CustomCP's Manifest must specify the provider, include the permissions allowed (e.g., read and/or write), and the provider must be exported. Here's an example:

<provider
    android:name="your.package.contentprovider.YourProvider"
    android:authorities="YourAuthority"
    android:readPermission="android.permission.permRead"
    android:exported="true" >
 </provider>

这篇关于访问不同的应用程序的自定义内容供应商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆