I am able to getting all messages with its data, type ,address,status etc. but I am not able restore these messages.I tried the following way. It inserts correctly but it shows current date not date of message recieved or send.Please help me how can I restore messages.
ContentValues values = new ContentValues();
values.put("address", "9878782944");
values.put("body", "foo bar");
values.put("date", "1322039220502");
values.put("type", "1");
values.put("status", "-1");
values.put("read", "1");
values.put("protocol", "0");
getContentResolver().insert(Uri.parse("content://sms"), values);
Well I am using the code below to restore SMS and in the "date" field I am giving Date in "yyyy-MM-dd kk:mm" format instead of long in millis, and it is working fine for me.
ContentValues values = new ContentValues();
values.put("address", numberFile);
values.put("body", bodyFile);
values.put("date", dateFile);
values.put("read", readStatusFile);
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
Also I am telling the insert function about the folder of SMS e.g. inbox. See if this can help you.
Convert Date in below given format. Android stores data in Milliseconds. So convert it before passing date to value.
ContentValues values = new ContentValues();
values.put("address", "8872743939");
values.put("body", "My Name is Kamal");
values.put("read", 1);
java.util.Date date = new java.util.Date(Message.getinstance().getDate());
Long Date = date.getTime();
values.put("date", Date.toString());
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
Related
I have created some events and stored the data in sqlite table but not the system calendar. However, i would like to have an alert/reminder for these events, which is similar to other events that stored in the system.
With reference to the code below, the details of the event are put in the "CalendarAlerts" table and in the broadcast receiver, the alertCursor is used to find the event data with start time close to the current time.
The code worked well if the event is stored in the system with a "long" eventID. But when i try to put data of my sqlite event in "CalendarAlerts" with a "string" eventID. It shows that the data is inserted into the table successfully but i would not query back the result in the "Alert Receiver" class.
Searched google for a while and it seems that not many people are talking on the topic of "CalendarAlerts". Great if anyone would share the experience on this issue.
Setting the AlarmManager
Uri alertUri = CalendarAlerts.CONTENT_URI;
long alarmMillis = (long) mStart - (long)(min*60*1000);
ContentValues alertValues =AlertUtils.makeContentValues(eventIdentifier,mStart, mEnd,alarmMillis, 0);
context.getContentResolver().insert(alertUri, alertValues);
public static ContentValues makeContentValues(String eventId, long begin, long end,
long alarmTime, int minutes) {
ContentValues values = new ContentValues();
values.put(CalendarAlerts.EVENT_ID, eventId);
values.put(CalendarAlerts.BEGIN, begin);
values.put(CalendarAlerts.END, end);
values.put(CalendarAlerts.ALARM_TIME, alarmTime);
long currentTime = System.currentTimeMillis();
values.put(CalendarAlerts.CREATION_TIME, currentTime);
values.put(CalendarAlerts.RECEIVED_TIME, 0);
values.put(CalendarAlerts.NOTIFY_TIME, 0);
values.put(CalendarAlerts.STATE, CalendarAlerts.STATE_SCHEDULED);
values.put(CalendarAlerts.MINUTES, minutes);
return values;
}
AlertReceiver.java
Cursor alertCursor = cr.query(CalendarAlerts.CONTENT_URI, ALERT_PROJECTION,
(ACTIVE_ALERTS_SELECTION + currentMillis), ACTIVE_ALERTS_SELECTION_ARGS,
ACTIVE_ALERTS_SORT);
You can use this code which works for me :
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, "description");
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
// default calendar
values.put(CalendarContract.Events.CALENDAR_ID, 1);
//for one hour
values.put(CalendarContract.Events.DURATION, "+P1H");
values.put(CalendarContract.Events.HAS_ALARM, 1);
// cr.delete(CalendarContract.Events.CONTENT_URI, null,null);
// insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
I'm working on an app that create contact using Intent. Why using Intent you will ask : Because It's to let user modify the contact as they want before registering.
My problem is while I'm using StructuredName or StructuredPostal, for other CommonDataKinds it's working well. Here's my code:
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
ContentValues row1 = new ContentValues();
row1.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");
row1.put(ContactsContract.CommonDataKinds.Organization.TITLE, "position");
data.add(row1);
ContentValues row3 = new ContentValues();
row3.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
row3.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_HOME);
row3.put(ContactsContract.CommonDataKinds.Website.URL, "website");
data.add(row3);
Here is StructuredPostal
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
data.add(row5);
i = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(i);
Due to this problem I momentarily did a patch :
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
i.putExtra(ContactsContract.Intents.Insert.POSTAL, "street, code city, country");
Someone knows what's wrong?
Thanks for answers :)
For the StructuredPostal add the type. And use a different mimetype. Do not add null values for the address fields like street etc.
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
data.add(row5);
StructuredName does not work for me either.
For using StructuredName in the Intent.INSERT you should add both Insert.Name and data row for StructuredName
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
Same goes for StructuredPostal. You need to add Insert.POSTAL and the StructuredPostal data row.
The Insert.Name is taken as the DISPLAY_NAME and the rest of the fields are used for StructuredName. The Insert.POSTAL is used as formatted address. This is why you need to provide both data.
To see how android processes your Intent, check method parseStructuredNameExtra in this file
I would handle sms with my app.
I use this code to insert the message into provider android:
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("date", System.currentTimeMillis());
values.put("body", text);
values.put("type", 1);
values.put("read", 1);
if (Build.VERSION.SDK_INT >= 19)
values.put(Inbox.DATE_SENT, System.currentTimeMillis());
context.getContentResolver().insert(Uri.parse("content://sms"), values);
The message is insert correctly but when i see it i reply an error.
The date of receipt is correct, but the date of dispatch is not correct (it's set to 1 January 1970).
How can i set it?
Thank you!
I want tosave operator Name also but i can't find any appropriate tag for that Any body please help me on This.
ContentValues values = new ContentValues();
values.put("address", originatingAddress);
values.put("body", sampleContent);
values.put("date", timeStamp);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
For getting operator name you can use:
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
String OperatorName = manager.getNetworkOperatorName();
and then use it in your code as you want OR like below:
ContentValues values = new ContentValues();
values.put("address", originatingAddress);
values.put("body", sampleContent);
values.put("date", timeStamp);
values.put("body", OperatorName);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
I need to receive sms from this a particular number, encrypt it and later sent in the inbox. This way my message is protected from reading by anyone else who handles my phone.
Try something like this
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
I hope this helps