Accessing custom content provider from different app

不想你离开。 提交于 2019-11-29 05:56:00

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. Specify the desired content provider (CustomCP) from within AppA by using a ContentProviderClient:

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

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

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

    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.

  3. 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>
    

in the manifest file, make sure that your

"provider android..>"
is inside your
 "application .. /application>" 

hope that helps

After creating the content provider , specify the content provider in the manifest file. You can mention content provider using the tag. Inside the provider tag dont forget to mention the name and authorities attributes. This declaration should be ..

<provider
        android:name="pakgName.ProviderClassName"
        android:authorities="pakgName.ProviderClassName"
        android:multiprocess="true" >
    </provider>

Here what you mention in the authorities attribute that should be match when you try to get the data from the provider.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!