How to use own ContentProvider to other application? - android

I have created a my own ContentProvider by extending class ContentProvider with all its abstract methods.
I am able to use MyContentProivder in my application where I created it,but I
am not getting how can I use it in other application.
This question might have been asked many times but I am really not getting any information.
Please Help

If you have your Custom ContentProvider you can use it in other application with its URI.
To insert,
ContentValues values = new ContentValues();
values.put("title", "lalit");
values.put("isbn", "0470285818");
Uri uri = getContentResolver().insert(Uri.parse(URI), values);
To read,
Uri allTitles = Uri.parse(URI);
Cursor c = managedQuery(allTitles, null, null, null, null);
In the same way you can delete, update using query.

Related

Confusion in ContentPorvider's CRUD operations

I am new to contenProvider and am reading a tutorial on how to do CRUD operations on UserDictionary.
For Query:
resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);
For insert:
resolver.insert(UserDictionary.Words.CONTENT_URI, values); //ContentValues values = new ContentValues();
For update:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
long noUpdated = resolver.update(uri, values, null, null);
for delete:
long noDeleted = resolver.delete(Words.CONTENT_URI,
Words.WORD + " = ? ", new String[]{"Zaphod"});
My confusion is in update and delete operations.
In update: why is it using Words.CONTENT_URI in withAppendedId() ? shouldn't it be using UserDictionary.Words.CONTENT_URI ?
Also in delete: its not using withAppendedId(). Still why is it using Words.CONTENT_URI ?
In your example UserDictionary.Words.CONTENT_URI is what identifies the data in a provider, so the data location, the appended Id is what identifies a unique record.
So, by your examples:
1) For Query, is using UserDictionary.Words.CONTENT_URI to return all the records under this content Uri address. If you wanted to return only one specific record, from which you already know its Id, then it could also be as follows:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.query(uri, projection, null, null, null);
3) For Insert, no Id is used because the record doesn't exist yet. What the operation does is to insert a new record into the database, and the return value will be the new record Uri, which will include the new Id.
3) For Update, the appended Id is used to identify which specific record must be updated. Updating UserDictionary.Words.CONTENT_URI without a record Id would result in the update of all the records.
4) For Delete, your example is deleting all the records where the column Words.WORD has the value "Zaphod", which may result in 0, 1, or multiple records being deleted.
If you wanted to delete only 1 specific record from which you know its Id, then it would be as next:
Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
resolver.delete(uri, null, null);
Answering your last question, there is no difference between Words.CONTENT_URI and UserDictionary.Words.CONTENT_URI. The UserDictionary section is a qualifier, which can be removed if has been added as an import on top of the java file, this allows not having to type it all the time, but at the end both are actually the same thing and makes no difference.

Android create second database after first database is created

So I created first database, and now I need to create separate database, zipped it and send it to server.
I'm just wondering if it is doable?
And how can I do it?
Here's the basic logic of what you want to do, it'll have to be supplemented with specific code from here: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
and here: https://developer.android.com/reference/android/database/Cursor.html
and here: https://developer.android.com/reference/android/content/ContentValues.html
SQLiteDatabase database1 = SQLiteDatabase.openDatabase(<PATH TO DATABASE1>, null, 0);
SQLiteDatabase database2 = SQLiteDatabase.openOrCreateDatabase(<WHEREVER YOU WANT DATABASE2 TO GO>, null);
Cursor cursor = database1.query(<You'll really just have to fill this in with what you're pulling from database1, it's way too specific>);
ContentValues contentValues;
while (cursor.moveToNext()) {
contentValues = new ContentValues();
contentValues.put(<Column key>, <Column value from cursor>)
// I.E. contentValues.put(ID_KEY, cursor.getString(cursor.getColumnIndex(ID_KEY));
// <INSERT THE REST OF YOUR COLUMNS INTO contentValues>
database2.insert(<TABLE_NAME>, null, contentValues);
}
cursor.close();
database1.close();
database2.close();
To send it, that depends on way too many things for me to address. You have the database file now, send it using whatever protocol you please. If you don't know how to do this, search for "Android send File to Server" (try with an without quotes).

Android SMS not displayed even after inserting into database correctly

I am developing an SMS backup and restore app. I am able to backup the SMSs into a CSV file. However, when I try to restore the SMSs, the SMSs are stored perfectly into the SMS database. But they are not visible into the Messaging App in Android.
I am using the Content Provider for backup and restoring the SMSs. Following is the code I am using for Restoring the SMSs.
CSVReader reader = new CSVReader(new FileReader(csvFile));
Uri uri = Uri.parse("content://sms/");
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
ContentValues cv = new ContentValues();
cv.put("_id", nextLine[0]);
cv.put("thread_id", nextLine[1]);
cv.put("address", nextLine[2]);
cv.put("person", nextLine[3]);
cv.put("date", nextLine[4]);
cv.put("protocol", nextLine[5]);
cv.put("read", nextLine[6]);
cv.put("status", nextLine[7]);
cv.put("type", nextLine[8]);
cv.put("reply_path_present", nextLine[9]);
cv.put("subject", nextLine[10]);
cv.put("body", nextLine[11]);
cv.put("service_center", nextLine[12]);
cv.put("locked", nextLine[13]);
this.getContentResolver().insert(uri, cv);
}
Please Let me know, what mistake I am doing. I am trying to work things out for past 1 week, still I don't know my Mistake in the Code. What am I missing in the code?
Do not include _id and thread_id in your inserts. After all records are inserted, do the following to trigger the conversations table threads update:
getContentResolver().delete(Uri.parse("content://sms/conversations/-1", null, null);

Inserting data in calls content provider fails calls application

I am trying to insert data into call log and retrieving it back the application runs fine but no data is displayed in the activity my code is as follows.
ContentValues values = new ContentValues();
values.put(Calls.CACHED_NAME, "Chaitanya");
Uri uri = getContentResolver().insert(Calls.CONTENT_URI, values);
The uri is also receives a value but the cursor doesn't return any record.
Cursor cursor = getContentResolver().query(Calls.CONTENT_URI,
projection, null, null, null);
This cursor is then used to display the data.
After the execution of program if calls or calllog application is launched it shows exception.
What could be the possible problem with the code?
Thanks in advance
You added just a name into database. Try adding some more informations like call type, date, duration ...

How to update datas with ContentProvider?

How can I execute this sql with Content Provider as below:
update dtrips set dtp_day_idx=dtp_day_idx+2 where tp_id=1
My java code is like this
DTrip dTrip = new DTrip();
ContentValues values = createContentValues(dTrip);
values.put("dtp_day_idx" ,...);
String select ="tp_id="+tripId;
mContentResolver.update(DTripColumns.CONTENT_URI, values, select, null);
Can anyone help me fix the code?
Thanks.
Append the ID for the row to the content URI like this: (assuming DTripColumns.CONTENT_URI is the content URI of your provider)
final Uri uri = ContentUris.withAppendedId(DTripColumns.CONTENT_URI, tripId);
mContentResolver.update(uri, values, null, null);
Here is more information: http://developer.android.com/guide/topics/providers/content-provider-basics.html

Categories

Resources