Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer above was a litle confusing for me. But I got it now. I want to post my solution as well. Maybe for someone it's more better to understand.</p> <p>First App A is the App that has the SQLite-Database and the "Custom Content Provider". App B uses with a ContentResolver the databse from App A.</p> <p>This is the AndroidManifest.xml-File from App A:</p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.test" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;permission android:name="de.test.READ_DATABASE" android:protectionLevel="normal" /&gt; &lt;permission android:name="de.test.WRITE_DATABASE" android:protectionLevel="normal" /&gt; &lt;application android:debuggable="true" ... &gt; ... ... &lt;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" /&gt; ... ... &lt;/application&gt; </code></pre> <p></p> <p>Ok and this is the AndroidManifest.xml-File from App B. Important is the part with "uses-permission":</p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.test.testercontentprovider" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="de.test.READ_DATABASE" /&gt; &lt;uses-permission android:name="de.test.WRITE_DATABASE" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="de.test.testercontentprovider.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; </code></pre> <p></p> <p>And the Code of the ContentProvider for App A looks like this:</p> <pre><code>public class TestContentProvider extends ContentProvider { public static final String AUTHORITY = "de.test.TestContentProvider"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + "nameoftable"); @Override public boolean onCreate() { ... return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub return null; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub return null; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } } </code></pre> <p>And the Code for the ContentResolver from App B:</p> <pre><code>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"; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver cr = getContentResolver(); // show entries of db listEntries(cr); } @Override public 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(); } } </code></pre> <p>I hope this can help someone to understand it better.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload