I have been trying to do this using content://sms/sim URI. But i can just access the data base, but not add to it. I actually want to implement 'copy to sim' functionality in my application.
This is my code snippet:
ArrayList listName=new ArrayList();
ArrayList listContactId=new ArrayList();
ArrayList listMobileNo=new ArrayList();
ArrayList listEmail=new ArrayList();
Uri simUri = Uri.parse("content://sms/sim");
Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);
String[] coloumnName=new String[cursorSim.getColumnCount()];
for(int i=0;i<cursorSim.getColumnCount();i++)
{
coloumnName[i]= cursorSim.getColumnName(i);
Log.i("Coulmn name -------!!!!!----------------",coloumnName[i]);
}
while (cursorSim.moveToNext()) {
listName. add(cursorSim.getString(cursorSim.getColumnIndex("name")));
listContactId. add(cursorSim.getString(cursorSim.getColumnIndex("_id")));
listMobileNo. add(cursorSim.getString(cursorSim.getColumnIndex("number")));
listEmail.add(cursorSim.getString(cursorSim.getColumnIndex("emails")));
}
This just allows me to read messages.
When i try to insert data.
Code snippet:
//Insert
ContentValues values = new ContentValues();
// values.put("name", "One");
values.put("address", "1111111111");
values.put("body", "Its a great day. Thats is been expected since i was born");
values.put("date", "1312434417006");
Uri newRowUri = getContentResolver().insert(simUri , values);
09-02 17:35:07.209: ERROR/SmsProvider(1476): Invalid request: content://sms/icc
I have given necessary permissions in my manifest file.
Can anyone suggest me how to do it. I even want to know whether this can be done, are there enough permissions to do this also.
Thanks
Vaishnavi
to read an sms from the phone you can use the regular api
Cursor cursor = context.getContentResolver().query(
SMS_INBOX_CONTENT_URI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
WHERE_CONDITION,
null,
SORT_ORDER);
now iterate through the cursor & get the message
you can use the SmsContentProvider to save the message in another folder.. (say the sim card)
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
ofcourse there are some loose ends which needs to be tied up..
its important to remind you to be careful while using content providers check this link
Related
I wanna make an android app that runs in background and check if there is a miscall or new massage and if it is true do something.How can I run my app in background (it does not close when I click back button) and how to access miscall and new message state I am an amateur in android.
Thanks!
For not seen sms messages there is a column in content://sms that you can check in order to determine if a message has been seen.
Column name is "seen".
example of calling it and checking:
ContentResolver mContectResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
String[] mProjection = {
"_id",
"address",
"person",
"date",
"body",
"protocol",
"seen"
};
Cursor cursor = mContectResolver.query(uri, mProjection, null, null, null);
cursor.moveToFirst();
for(int i = 0 ; i<cursor.getCount() ; i++){
if(cursor.getString(cursor.getColumnIndexOrThrow("seen")).equalsIgnoreCase("1")){
//Message has not been seen
}
cursor.moveToNext();
}
cursor.close();
How do I update the display name for a contact? The operation in the code below completes without throwing anything and appears to work - that is, when I requeried the ContactsContract.Contact table, a row came back with the name changed. However, when I tried running the stock "people" app on my tablet, it crashed. Evidentally I did something wrong.
Here is the code. Early on, it fetches an id from the aggregate contacts as follows, where key is the lookup_key:
String[] projection = new String[] {
Contacts._ID, // 0
Contacts.DISPLAY_NAME, // 1
};
Uri uri = Uri.parse (Contacts.CONTENT_LOOKUP_URI + "/" + key);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query (uri, projection, null, null, null);
if (!cursor.moveToNext()) // move to first (and only) row.
throw new IllegalStateException ("contact no longer exists for key");
origId = cursor.getLong(0);
cursor.close();
Then, after the user has done his edits, I call this block of code to update the display_name:
ArrayList<ContentProviderOperation> opers = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = null;
String[] args = { Long.toString (origId) };
builder = ContentProviderOperation.newUpdate (Data.CONTENT_URI);
builder.withSelection (RawContacts.CONTACT_ID + "=?", args);
builder.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, name);
opers.add(builder.build());
ContentProviderResult[] results = null;
try {
results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, opers);
} catch ...
I realize I don't need the ContentProviderOperation for this example; that's for later when I have more stuff to update.
To be honest, I'm pretty confused about which ID I'm actually using. The names aren't that clear to me and I may be using the wrong ID for this operation.
For what it's worth, looking at results after the update I saw a result code of 5. I can't find any documentation for that, so have no idea if that is significant.
The IDs (and altering contacts in general) can be pretty confusing... I had some dramas getting my head around them as well.
Here is some working code I use for updating. The main difference I can see is how you are declaring the raw ID; it needs to be included in as a content value.
Cursor cursor = _context.getContentResolver().query(contactUri,
new String[] { Contacts._ID }, null, null, null);
try {
if (cursor.moveToFirst()) {
String rawContactId = cursor.getString(0);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ContentValues contentValues = new ContentValues();
contentValues.put(Data.RAW_CONTACT_ID, rawContactId);
contentValues
.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(
ContactsContract.CommonDataKinds.Phone.NUMBER,
phoneNumber);
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValues(contentValues).build());
String contactId = contactUri.getLastPathSegment();
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {
contactId,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE })
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
newName).build());
result = _context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops);
}
} finally {
cursor.close();
}
hopefully it helps!
Answer above is generally correct. However, when I inserted
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
and then tried to do newUpdate
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
with code as above - it made contact displayed in Contacts app with name mixed of old and new data. I found out inserting and updating ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME for example works as I expect. In Contacts app on my Android 4x when editing contact I cannot see family and given separately, look to me DISPLAY_NAME is made of them by Android.
I want to display a list of the contact names from the SMS inbox and outbox (like in the native messaging app). I have come up with the following code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex(columns[0]));
sms += address + " ";
String contact=address;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
startManagingCursor(cs);
if(cs.getCount()>0)
{
cs.moveToFirst();
contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
listItems.add(contact);
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
This works when I run the application in an emulator, but when I try to run it on a phone I get a NullPointerException. If I double-click on the error message in the LogCat the following line gets highlighted:
if(cs.getCount()>0)
What is the problem here?
From the documentation for ContentResolver.query(...)
A Cursor object, which is positioned before the first entry, or null
You must always check for null when using a ContentResolver to query for data.
Also from the same documentation.
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
So instead of:
Cursor cs= getContentResolver().query(uri, new String[ {PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
Do:
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"=?",new String[]{address},null);
My first guess would be that cs is null. As to why it is null, I suppose that the carrier/manufacturer could have changed how this is implemented on your device. Without knowing too much about how these systems work, it looks like you are querying against some kind of database and then using a cursor to iterate over the data. If the representation has been changed, the cursor will break. My suggestion would be to see if there is some other solution that is more Google/API driven (as opposed to reading the raw data).
In my application I need to read sms coming from only a number, and when i receive it I need to set it as read automatically, without setting it in the sms android application, but from my app. How can I do?
Thanks!
Let me update this:
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/"),values, "_id="+SmsMessageId, null);
SmsMessageId is _id of message, which you find in SMS database.
A short example:
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
// Retrieve sms
// see column "address" for comparing
// Then update the sms and set the column "read" to 1
}
I'm torn about how to implement this because Content Provider URI querys do not support the simple SQL "DISTINCT" query method to return a cursor to the Artists of the songs in the mediastore, removing any duplicate entries.
I can query and get a cursor to all the artists, I'm just torn as to how to remove the dupes, or simply not show them.
I've tried using matrixcursor to construct a new cursor with the dupe entries removed, but it's slow (build an array of artists, if in that array don't copy to the new matrixcursor, etc.)
Can anyone recommend a better solution, or point me in the proper direction??
I've also thought about pre-loading the information i need into an array of objects - I'm just concerned with memory overhead in my application.
Thank You for any help you may provide.
The easiest way to get a list of all the artist (and albums is the same method) is to use the MediaStore.Audio.Artist for the quest. For Example something like this would get and show all the artist:
String[] proj = {MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
musiccursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
String[] from= new String[]{ MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
int[] to = new int[] { R.id.songname, R.id.rowlength, R.id.rowartist };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.musicrow, musiccursor, from, to);
SongsView.setAdapter(adapter);
Where SongsView would be your list to display them in. Everything else is using a simple Cursor adapter
Hope this helps
You might want to try creating stateful CursorWrapper, overriding the appropriate methods. Simply ensure every call to next() iterates through the cursor until it finds an artist name you consider appropriately unique, optionally storing seen artists in an ArrayList instance variable.
ArrayList<SongBeen> genresList=new ArrayList<SongBeen>();
String value=search+ "%";
String[] inValue=new String[] {value};
ContentResolver musicResolver = activity.getContentResolver();
/*Uri musicInUri = android.provider.MediaStore.Audio.Genres.INTERNAL_CONTENT_URI;
Cursor mInternalCursor = musicResolver.query(musicInUri, null, null, null, null);
*/
Uri musicExUri = android.provider.MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
Cursor mExternalCursor = musicResolver.query(musicExUri, null, MediaStore.Audio.Genres.NAME+ " like ?",
inValue, "LOWER(" + MediaStore.Audio.Genres.NAME + ") ASC");
Cursor[] cursors = {mExternalCursor};
final MergeCursor mMergeCursor = new MergeCursor(cursors);
if (mMergeCursor.moveToFirst()) {
do {
SongBeen been=new SongBeen();
long thisId= mMergeCursor.getLong(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID));
String thisTrack = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME));
been.setId(thisId);
been.setTrack(thisTrack);
genresList.add(been);
} while (mMergeCursor.moveToNext());
}
mMergeCursor.close();
return genresList;