This is code for get SMS log in a period of selected date but it returns empty cursor:
long lFrom = login.getTime();
long lTo= logout.getTime();
String[] whereValue = {String.valueOf(lFrom),String.valueOf(lTo)};
Cursor cursor = this.getContentResolver().query(allMessages, new String[]{"date", "type", "address"},
Telephony.Sms.DATE + " BETWEEN ? AND ?", whereValue, null);
Related
Trying to Delete just One(the Last) Message in SMS Using this Code :
Uri uriSMSURI = Uri.parse("content://sms/conversations/");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur.moveToFirst()) {
String pid0 = cur.getString(1);
/*Force Close Here, illegal State for the Cursor, Couldn't Read Row 0, Col -1, From CursorWindows :
String pid = cur.getString(cur.getColumnIndex("_id"));*/
String where = "thread_id=" + pid;
String[] Columns= cur.getColumnNames();
// do some process
Log.e("TAG", "content://sms/conversations/" + pid0 + "/" + pid);
mContext.getContentResolver().delete(Uri.parse("content://sms/conversations/" + pid0 + "/" + pid), null, null);
The Method getColumnNames() Return Only 3 Columns : "thread_id", "msg_count", "snippet", i am looking for "_id" wich is Assigned to each Message, i get a Force Close when trying to read "_id", What's wrong ?
Edit : i am able now to Get the Message id, but unable to Delete the Message, Nothing Happen :
String id0 = cur.getString(0);
mContext.getContentResolver().delete(Uri.parse("content://sms/" + id0), null, null);
What's Wrong ?
Thanks
Solved, the Problem was i was Querying "content://sms/conversations/" instead of "content://sms/",
Code :
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur.moveToFirst()) {
////Changed to 0 to get Message id instead of Thread id :
String MsgId= cur.getString(0);
mContext.getContentResolver().delete(Uri.parse("content://sms/" + MsgId), null, null);
Thanks
I am trying to develop a SMS counter app in which I want to count all SMS of the present date. i don't want to count total inbox SMS. I am using following code but not getting the exact result. It's counting total inbox SMS.
TextView view = new TextView(this);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
/*
int nsm = 0;
while(cur.moveToNext()){
nsm+= + cur.getCount();
}
*/
String sms = "";
while ( cur.moveToNext());{
// sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
// sms += cur.getString(2);
sms += "Total SMS in the INBOX : "+cur.getCount();
}
view.setText(sms);
setContentView(view);
I am a new learner. Thanks in advance.
If you are trying to count the number of sms received within the last 24h you may do something like this (not sure if this actually work because I didn't tested it, but you get the idea) :
Uri uriSMSURI = Uri.parse("content://sms/inbox");
long now = System.currentTimeMillis();
long last24 = now - 24*60*60*1000;//24h in millis
String[] selectionArgs = new String[]{Long.toString(last24);
String selection = "WHERE " + "date" + ">?";
Cursor cur = getContentResolver().query(uriSMSURI, null, selection, selectionArgs,null);
Then a simple cur.getCount() should get you the number of sms received.
Uri uriSMSURI = Uri.parse("content://sms/inbox");
long now = System.currentTimeMillis();
long last24 = now - 24*60*60*1000;//24h in millis
String[] selectionArgs = new String[]{Long.toString(last24)};
String selection = "date" + ">?";
String[] projection = new String[]{"date"};
Cursor cur = getContentResolver().query(uriSMSURI, projection, selection, selectionArgs,null);
String sms = String.valueOf(cur.getCount());
This is the Exact resul what i wanted thanks for giving me quick reply
I'm trying to make a many-to-many mapping of contacts to groups.
For example, if I have:
User 1, belongs to group 701, 702, 704
User 2, belongs to no groups
User 3, belongs to group 702
I'm hoping to get a relation that looks like this:
userID | groupID
1 | 701
1 | 702
1 | 704
3 | 702
I've tried this:
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, new String[] {
ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_SOURCE_ID
}, null, null, null);
But that doesn't quite work. The GROUP_SOURCE_ID column returns weird numbers that aren't the ID of any groups. Sometimes it even returns 0 or a negative number.
I could construct a mapping of this by going through each group, and finding all contacts in that group, but that would take a lot of queries, and I'm trying to stay fast (apparently, just those few queries are quite slow!).
Can anyone tell me how I can get this contacts-to-groups mapping in one query?
Thanks!
Cursor dataCursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DATA1
},
ContactsContract.Data.MIMETYPE + "=?",
new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}, null
);
By using this dataCursor you will get the contact_id and group_id of all contacts in the contact database.
Cursor groupCursor = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
new String[]{
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE
}, null, null, null
);
By using this groupCursor you will get the group_id and group_title of all groups in the contact database.
So if you want to get all groups associated with a contact_id the first get the dataCursor using suitable select statements. Using dataCursor you can get all the group_id associated with that contact_id. Now using groupCursor you can get the information about all groups associated with that specific contact.
A complete answer will be:
First the fetch the group cursor (same as the answer above)
Cursor groups_cursor= getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
new String[]{
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE
}, null, null, null
);
Store all the group_id and group_title in a groups HashMap using this code:
if(groups_cursor!=null){
while(groups_cursor.moveToNext()){
String group_title = groups_cursor.getString(1);
String id = groups_cursor.getString(0);
groups.put(id, group_title);
}
}
Then using the above answer's data_cursor, fetch contacts_ids and their group_ids.
Cursor dataCursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DATA1
},
ContactsContract.Data.MIMETYPE + "=?",
new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}, null
);
Now using the dataCursor and the groups HashMap.
if(dataCursor!=null){
while(dataCursor.moveToNext()){
String id = dataCursor.getString(0);
String group_id= dataCursor.getString(1);
String groupTitle = groups.get(group_id);
Log.d(TAG, "groupTitle : " + groupTitle + " contact_id: " + id );
}
}
public static HashMap<String, String> getContactsForGroup(String groupID, Activity activity){
Cursor dataCursor = activity.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ // PROJECTION
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME, // contact name
ContactsContract.Data.DATA1 // group
},
ContactsContract.Data.MIMETYPE + " = ? " + "AND " + // SELECTION
ContactsContract.Data.DATA1 + " = ? ", // set groupID
new String[]{ // SELECTION_ARGS
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
groupID
},
null
);
dataCursor.moveToFirst();
HashMap<String, String> map = new HashMap<>();
while (dataCursor.moveToNext()) //
{
String s0 = dataCursor.getString(0); //contact_id
String s1 = dataCursor.getString(1); //contact_name
String s2 = dataCursor.getString(2); //group_id
Log.d("tag", "contact_id: " + s0 + " contact: " + s1 + " groupID: "+ s2);
map.put(s0, s1);
}
return map;
}
All the examples I see for doing a query show only using one key. Is it possible to use two keys. I have tried the code below and it does not work. I am trying to find the row where date and time match the date and time in the table
public String getPrimaryID(int date, int time )
{
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ "time=" + time, null, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
String result=c.getString(id);
return result;
}
I think you need to AND your query:
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ " AND time=" + time, null, null, null, null);
try this :
Cursor c = db.query(TABLENAME, null, "date = ? AND time = ?", new String[] { date, time }, null, null, null, null);
or :
String query ="SELECT * FROM " + TABLENAME + " " + "WHERE "
"date = ? " +
"AND time = ? ";
Cursor c = db.rawQuery(query, new String[] {date, time});
Explanation : variables date and time will replace the ? characters on the query
I'm working on android application in which I take backup of all contact information and then restore, I retrieve all information of contact,
For example:
Display Name
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null)
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Phone Number
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
Similary
But I unable to get "Internet Call" value.
Kindly anyone tell in which class I will get information about Internet Call information.
Dont know if this is the best way, but it worked, I am fairly new new to android.
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS,
ContactsContract.CommonDataKinds.SipAddress.TYPE,
};
String selection =
ContactsContract.Data.MIMETYPE+" ='"
+ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE+"'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
Cursor cursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
It seems that the phone number is stored in misc information data and you have search on the mime type.
HTH
g.
i have tested. i am able to get internet call value. try below code.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur=cr.query(uri, null, null, null, sortOrder);
if(cur.getCount()>0){
while(cur.moveToNext()){
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
String internetWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] internetWhereParams = new String[]{id,ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE};
Cursor internetCur = cr.query(ContactsContract.Data.CONTENT_URI, null, internetWhere, internetWhereParams, null);
if (internetCur.moveToFirst()) {
String internetCall = internetCur.getString(internetCur.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS));
Log.e(TAG, "internet Call: " + internetCall);
} internetCur.close();
}
}
} cur.close();