Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing custom content provider from different app

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?

like image 740
tgAndroid Avatar asked Apr 19 '11 09:04

tgAndroid


People also ask

How can I access my content provider from another application?

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

Can an app access data from another app?

Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.

How can you access content provider's data into other application justify your answer with suitable example?

To access the content, define a content provider URI address. Create a database to store the application data. Implement the six abstract methods of ContentProvider class. Register the content provider in AndroidManifest.

How many content providers can an app have?

You can implement as many as you want, as you can see from the documentation here. To register a content provider, you need to add its corresponding <provider> tag in the Android Manifest. In most cases, however, you won't need multiple content providers. One is usually enough, as it can handle multiple tables.


1 Answers

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>
    
like image 123
PeteH Avatar answered Oct 11 '22 15:10

PeteH