I am developing an android app which backs up and restores the messages/conversations from device. It backup the messages, export file in the form of xml, and then later restore it. The only problem I am facing is the date/times of conversations. It is changed to current time at the time of restoration, but when I open any conversation, there time is correct. Have a look at photos.
Before backup:
After backup:
Code I am using for backup:
Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Cursor SMSL = contentResolver.query(Telephony.Sms.Inbox.CONTENT_URI, projection, null, null, null);
int msgscount = SMSL.getCount();
if (msgscount>0) {
msgs = new String[SMSL.getCount()][5];
int i = 0;
while (SMSL.moveToNext()) {
address = SMSL.getString(SMSL.getColumnIndex("address"));
body = SMSL.getString(SMSL.getColumnIndex("body"));
read = SMSL.getString(SMSL.getColumnIndex("read"));
date = SMSL.getString(SMSL.getColumnIndex("date"));
type = SMSL.getString(SMSL.getColumnIndex("type"));
msgs[i][0] = address;
msgs[i][1] = body;
msgs[i][2] = date;
msgs[i][3] = read;
msgs[i][4] = type;
Log.i("Date: ", String.valueOf(SMSL.getLong(SMSL.getColumnIndex("date"))));
i++;
}
SMSL.close();
}else{
msgs = new String[0][0];
Toast.makeText(getApplicationContext(),"No messages found!",Toast.LENGTH_LONG).show();
}
Code for restoring:
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/inbox");
//Uri uri = Uri.parse("content://mms-sms/conversations/");
ContentValues values = new ContentValues();
for (int i = 0; i < readMsgsFromFile.length; i++) {
values.put("address",readMsgsFromFile[i][0]);
values.put("body",readMsgsFromFile[i][1]);
values.put("date",readMsgsFromFile[i][2]);
values.put("read",readMsgsFromFile[i][3]);
values.put("type",readMsgsFromFile[i][4]);
contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values);
Log.i("Restoring: ",readMsgsFromFile[i][2]);
}
Thanks Mike M. I did find a solution and you are right, the conversation table is updated whenever a new message is received or sent by a user and the time of conversation is same as that message's (whether received or sent) time. But in case of writing messages through contentresolver query it does not work and the conversation time is current time at the time of writing. So what I did is add a temporary message in all of the conversations, right after messages are restored. And after that delete all the temporary messages, this will update the conversations time to last message time.
HashSet hs = new HashSet();
hs.addAll(list);
//list is the ArrayList<String> which contains the addressess of all the messages and
//through hashset we remove all the duplicates to get only the addressess once and hence we know the number of conversations and their addressess.
list.clear();
list.addAll(hs);
//Add some dummy message to each conversation
ContentValues values2 = new ContentValues();
for (int i = 0; i < list.size(); i++) {
values2.put("address",list.get(i));
values2.put("date_sent",readMsgsFromFile[0][1]);
values2.put("date",readMsgsFromFile[0][2]);
values2.put("type",readMsgsFromFile[0][3]);
values2.put("body","temp"); //this should be more unique
values2.put("read",readMsgsFromFile[0][5]);
values2.put("service_center","01010101");
contentResolver.insert(Telephony.Sms.CONTENT_URI, values2);
}
//Now deleting that message with body 'temp' from each conversation
Cursor c = contentResolver.query(Telephony.Sms.CONTENT_URI,null,null,null,null);
while (c.moveToNext()){
String body = c.getString(c.getColumnIndex("body"));
String mid = c.getString(0);
if (body.equals("temp")){
Log.i("Deleting ",mid);
getContentResolver().delete(Uri.parse(Telephony.Sms.CONTENT_URI+"/"+mid),null,null);
}
}
c.close();
This word 'temp' could be and should be more unique so that it is not mixed with actual message.
Related
I am trying to permanently remove a Android Contact Group and have used the Sync parameter and it always appears the record is simply marked as deleted and not physically removed. Can anyone explain how/when, if ever, the Contract group row is deleted permanently or show a snippet of code demonstrating how to do this? The records I am trying to remove are ones that I added, so they are not Read-Only.
Linked back to https://stackoverflow.com/a/21376905/5398898
My Delete Code:
private void RemoveGroup()
{
TextView tv = (TextView) this.findViewById(R.id.helloworld);
int[] startId = {10};//{6, 7, 8, 9, 10, 11};
String groupName = "My New Contacts";
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
for (int n = 0; n < startId.length; n++) {
groupCount = startId[n];
ContentValues values = new ContentValues();
values.put(ContactsContract.Groups._ID, groupCount);
try {
getContentResolver().delete(mUri, values.toString(),null);
} catch (Exception ex) {
tv.setText(ex.getMessage());
}
}
}
Result when reading the groups:
Image can be found here http://i.stack.imgur.com/5OOfc.png
You are building the correct Uri but not using it, try like this
Uri mUri = ContactsContract.Groups.CONTENT_URI;
mUri = mUri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
In my application i want to get sms/mms from device and display the messages in listview.By using the following code to get the all sms from device.
public void readSmsFromDevice() {
preferences = PreferenceManager
.getDefaultSharedPreferences(BackgroundService.this);
final_msg_time = preferences.getLong("msgtime", 0);
Uri uriSMSURI = Uri.parse("content://sms/");
String[] projection = { "address", "body", "date", "type" };
String where = "date" + ">" + final_msg_time;
Cursor cur = getContentResolver().query(uriSMSURI, projection, where,null, "date");
while (cur.moveToNext()) {
if(ProfileFragment.stop)
{
break;
}else{
try {
//
Message mess1=new Message();
try{
String _id = cur.getString(cur.getColumnIndex("_id"));
mess1.setId(_id);
}catch(Exception e)
{
mess1.setId("null");
}
try{
String number = cur.getString(cur.getColumnIndex("address"));
number = number.replaceAll("[\\W]", "");
if (number.trim().length() > 10) {
mess1.setNumber(number.substring(number.length() - 10,
number.length()));
mess1.setAddress(number.substring(number.length() - 10,
number.length()));
} else {
mess1.setNumber(number);
mess1.setAddress(number);
}
}
catch(Exception e){}
mess1.setBody(cur.getString(cur.getColumnIndex("body")));
String type = cur.getString(cur.getColumnIndex("type"));
Long millisecond = Long.parseLong(cur.getString(cur
.getColumnIndex("date")));
String dateString = DateFormat.format("yyyy/MM/dd hh:mm:ss a",
new Date(millisecond)).toString();
mess1.setDate_millis(millisecond);
mess1.setDate(dateString);
mess1.setType(type);
mess1.setmessagetype("sms");
messages.add(mess1);
} catch (Exception e) {}
}
}
cur.close();
}
By using this method i am getting all sms from device.But my question is how to differenciate group message.In group message one message sent different contact numbers(senders).So in normal message application group message displayed in separate column and single message displayed in separate column.So my application also i have to display the messages like message application.So in this cursor how to identify group message?Is there any column is available to identify group message?So please suggest me how to do taht.Thanks In Advance.....
the thread THREAD_ID column will give you all the messages in the same conversation. You can then use the address column to differentiate the senders of the messages in the group message.
You should also use the Telephony class that was introduced in API 19 instead of the content resolver. https://developer.android.com/reference/android/provider/Telephony.html
I tried to get sms list and below code works well in some devices but not working in other devices. Details tested with four devices using below code.
LG optimus one[android 2.2] - works well. SS galaxy s3[android4.0.4] - works well. SS galaxy s2[android
2.3.5] - not working. SS galaxy s2[android 4.0.4] - not working.
It seems that result depends on devices not android version because two devices with same android version[4.0.4] show differently. Symptom in the devices not working is that c.getCount() = 0 even if they have many sms. The query returns nothing. Why these are? How can I get sms list in galaxy s2 as well?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String smsMsgBody = null;
String smsMsgAddress = null;
String smsMsgDirection = null;
Uri uri = Uri.parse("content://sms");
// Uri uri = Uri.parse("content://sms/inbox");
// Uri uri = Uri.parse("content://sms/conversations/");
Cursor c= getContentResolver().query(uri, null, null,null,null);
// startManagingCursor(c);
if (c.getCount() > 0)
{
String count = Integer.toString(c.getCount());
while (c.moveToNext())
{
smsMsgBody = c.getString(c.getColumnIndex("body"));
smsMsgAddress = c.getString(c.getColumnIndex("address"));
smsMsgDirection = c.getString(c.getColumnIndex("type"));
// Do things using above sms data
}
}
c.close();
}
The content://sms/ is not a supported content provider. It is a hidden method, and may not be available in all devices. In Android 4.4 KitKat devices, you can use the code in this answer to do it:
public List<String> getAllSms() {
List<String> lstSms = new ArrayList<String>();
ContentResolver cr = mActivity.getContentResolver();
Cursor c = cr.query(Telephony.Sms.Inbox.CONTENT_URI, // Official CONTENT_URI from docs
new String[] { Telephony.Sms.Inbox.BODY }, // Select body text
null,
null,
Telephony.Sms.Inbox.DEFAULT_SORT_ORDER // Default sort order);
int totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
lstSms.add(c.getString(0));
c.moveToNext();
}
}
else {
throw new RuntimeException("You have no SMS in Inbox");
}
c.close();
return lstSms;
}
I don't believe that there is a documented method that will work across all devices right now.
I want to get the information about the messages from the inbox on the basis of a particular number.I am using following code for achieving the goal but it is not working in the expected way:
public void SendTheSmsToTheFolder(String NameOfContact,String Number,String FolderAddress,long TimeLimit)
{
m_NameOfContact = NameOfContact;
String SMS_URI_INBOX = "content://sms/inbox";
Uri l_uri = Uri.parse(SMS_URI_INBOX);
Cursor l_SendTheSmsToTheFolderCursor = getContentResolver().query(l_uri, null, "address=?",new String[]{Number.trim()},null);
if (l_SendTheSmsToTheFolderCursor.moveToFirst())
{
int l_index_Address = l_SendTheSmsToTheFolderCursor.getColumnIndex("address");
int l_index_Person = l_SendTheSmsToTheFolderCursor.getColumnIndex("person");
int l_index_Body = l_SendTheSmsToTheFolderCursor.getColumnIndex("body");
int l_index_Date = l_SendTheSmsToTheFolderCursor.getColumnIndex("date");
do
{
String l_strAddress = l_SendTheSmsToTheFolderCursor.getString(l_index_Address);
String l_strbody = l_SendTheSmsToTheFolderCursor.getString(l_index_Body);
long l_longDate = l_SendTheSmsToTheFolderCursor.getLong(l_index_Date);
Log.v("Message: ","Body of the message is "+l_strbody);
} while (l_SendTheSmsToTheFolderCursor.moveToNext());
}
}
I am quite sure that the message having that particular phone number is present in the box and i have cross checked this fact.But the here,i cursor count is always showing 0.I don't know what is the problem.I have searched a lot but am not able to figure it out.Please help me.Thanks in advance.
If you want to search messages from a particular number, you can try this code.
Uri uri = Uri.parse("content://sms/inbox");
ContentResolver contentResolver = context.getContentResolver();
String where = "address="+ phNum; //here you can use that particular number
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,null);
It has worked for me. let me know if it solved your issue.
What I am trying to accomplish here is to make a Cursor that could be used in an android ListView. I'm reading values directly from multiple files and have to feed them to the cursor. I tried to use MatrixCursor but I can't get it to work with arrays. I have posted my attempt at it so far below and I'm open to all new suggestions. Is there a simpler way to do this?
static MatrixCursor getnameList() {
ArrayList<String> fsitem = getfsiList();
MatrixCursor cursor;
cursor = null;
for (int i = 0; i < fsitem.size(); i++) {
try {
File root = new File(Environment.getExternalStorageDirectory()
.getName() + "/" + fsitem.get(i));
if (root.canRead()) {
File namefile = new File(root, ".name");
FileReader namereader = new FileReader(namefile);
BufferedReader in = new BufferedReader(namereader);
String name = in.readLine();
String id = in.readLine();
String info = in.readLine();
String[] fsii = new String[3];
fsii[0]= name;
fsii[1]= id;
fsii[2]= info;
cursor.addRow(fsii); //crashes here on running.
}
} catch (IOException e) {
Log.e("NameManager.java : ", ("Error!! Not Writable!!"
+ Environment.getExternalStorageDirectory().getName()
+ "/" + fsitem.get(i)));
}
}
This code compiles but crashes at cursor.addRow(fsii);:
with 02-24 21:16:49.589: E/AndroidRuntime(3895): at com.manager.abcd.r1223.NameManager.getnameList(NameManager.java:81).
I'm thinking this is a problem with MartixCursor not supporting arrays, but I might be wrong. Any ideas?
If this is all the code then it is normal because you try to add a row on a null cursor(you never initialize cursor) and probably get a NullPointerException.
Initialize the MatrixCursor before you enter in the for loop:
String[] columnNames = {"col1", "col2", "col3"};
MatrixCursor cursor = new MatrixCursor(columnNames);
Check the docs.