How to get all SMS in android - android

I am trying to fetch all sms(both inbox,sent sms) in android.
I have tried with these
Uri uri_sent = Uri.parse("content://sms");
Cursor c_sent = getContentResolver().query(uri_sent, null, null, null,
null);
startManagingCursor(c_sent);
// Read the inbox sms data and store it in the list
if (c_sent.moveToFirst()) {
for (int i = 0; i < c_sent.getCount(); i++) {
SmsDataClass sms = new SmsDataClass();
sms.setAddress(c_sent.getString(
c_sent.getColumnIndexOrThrow("address")).toString());
sms.setBody(c_sent.getString(
c_sent.getColumnIndexOrThrow("body")).toString());
sms.setDate(c_sent.getString(
c_sent.getColumnIndexOrThrow("date")).toString());
sms.set_id(c_sent
.getString(c_sent.getColumnIndexOrThrow("_id"))
.toString());
sms.setType(c_sent.getString(
c_sent.getColumnIndexOrThrow("type")).toString());
// sms.setTitle(c.getString(c.getColumnIndexOrThrow("title")).toString());
all_sms_data.add(sms);
c_sent.moveToNext();
}
} else
c_sent.close();
it sometimes works but it doesn't work regularly. Sometimes it shows null pointer exception on c_sent.getColumnIndexOrThrow("address"),c_sent.getColumnIndexOrThrow("body") ....
What is the problem ???
I dont understand.
Before oneday it works fine when today i have nullpointer exception.
Why???

Related

Get the unread mail count gmail in Android

I want to get an int with the number of unread emails in the accounts of the device. I have seen that there is a new way to do this using the "Gmail Labels Public API"
http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html
I have read the documentation and downloaded the sample application and it really works. But I have two problems: (
My intention is to get an int with the number of unread conversations, i try this:
public static int getUnreadGmailCount(Context context) {
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(GmailContract.Labels.getLabelsUri("ensisinfo102#gmail.com"),
null,
null, null,
null);
if (cursor == null || cursor.isAfterLast()) {
Log.d(TAG, "No Gmail inbox information found for account.");
if (cursor != null) {
cursor.close();
}
return 0;
}
int count = 0;
while (cursor.moveToNext()) {
if (CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(cursor.getString(cursor.getColumnIndex(CANONICAL_NAME)))) {
count = cursor.getInt(cursor.getColumnIndex(NUM_UNREAD_CONVERSATIONS));
System.out.println("count is====>"+count);
break;
}
}
cursor.close();
return count;
}
but not works, always returns "0",But in gmail i have 3 unread messages
really appreciate any help
thanks and regards
You can get a label and check the messagesUnread. The INBOX label is probably what you want:
Request
GET https://www.googleapis.com/gmail/v1/users/me/labels/INBOX?access_token={ACCESS_TOKEN}
Response
{
"id": "INBOX",
"name": "INBOX",
"messageListVisibility": "hide",
"labelListVisibility": "labelShow",
"type": "system",
"messagesTotal": 4527,
"messagesUnread": 4498,
"threadsTotal": 4168,
"threadsUnread": 4154
}
Please read carefully this document and also check this one

Restoring backed up messages, conversations times are incorrect

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.

How to get sms list depending on various devices

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.

Reading mms details programmatically and display in listview

i need to get all the MMS Data detalis like mms_image, address,date and type.
i am using following logic. In this i am using two cursors, one for images and other for remaining fields. but the size of two cursors are different. so, i am unable to match both image and other fields.
//for date,address,type
Cursor curPdu = getContentResolver ().query(Uri.parse("content://mms"), null, null, null, null);
while(curPdu.moveToNext())
{
String id = curPdu.getString (curPdu.getColumnIndex ("_id"));
String date = curPdu.getString (curPdu.getColumnIndex ("date"));
mms_add.add(getAddressNumber(Integer.parseInt(id)));
int type = Integer.parseInt(curPdu.getString(curPdu.getColumnIndex("m_type")));
mms_type.add((type==128)?"2":"1");
mms_date.add(getDate(Long.parseLong(date)));
}
//for image
Cursor curPart = getContentResolver (). query (Uri.parse ("content://mms/part"), null, null, null, null);
while(curPart.moveToNext())
{
coloumns = curPart.getColumnNames();
if(values == null)
values = new String[coloumns.length];
for(int i=0; i< curPart.getColumnCount(); i++)
{
values[i] = curPart.getString(i);
}
if(values[3].equals("image/jpeg"))
{
mms_image.add(GetMmsAttachment(values[0],values[12],values[4]));
}else{
mms_image.add("null");
}
}
so, please guide me how to get all the details using one cusor if possible.
You can try the solution and url provided here.
And may i know why you need 2 cursor ? I assume there is some mms without image attached so that's the reason you get a different count.

Receiving unknown count of results from SQLite db

Basically this is not a problem in itself, my code works so far.
What I have is a App, that lets a user log in and depending on his ID in the db, he gets displayed his saved notes. For this view I have this part of code:
title = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
MyDbHandler dbh = new MyDbHandler(this);
for(int i = 0; i < 999; i++) {
content = dbh.getNoteTitle(id, i); //getNoteTitle(int, int) returns String
if(content != null && content != "0")
title.add(content);
else
break;
}
list.setAdapter(title);
As I said, this works so far.
Thing is - I am very unhappy with the use of ' break; ' here, as I learned during education, this shouldn't be used.
Is there a smoother way to approach this issue?
Also ' content != "0" ' should be ' ! content.equals("0") ' normally, right? But that one doesn't work then... Why is this?
I am not sure what are you trying to do. First of all you should use "equal" method for Strings. The condition "content != "0" will always be true, because you are comparing 2 different objects. The condition "! content.equals("0")" should return true most of the time (when the value is not "0") and probably you should use the debugger to see exactly what is the value of content.
Second if you want to take all the notes from the database and show them to the user you should have first a method in the SQLiteOpenHelper similar to (it is not efficient to interrogate the database for each item, plus the separation of concerns):
public ArrayList<String> getNotesList (int userID){
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TABLE_NAME, new String[] {MyDbHandler.COLUMN_NOTE_TITLE}, MyDbHandler.userID + "=" + userID,null, null, null, null);
ArrayList<String> list = null;
String noteTitle;
if (c != null && c.moveToFirst())
{
list = new ArrayList<String>(c.getCount());
for (int i = 0; i < c.getCount(); i++)
{
noteTitle = c.getString(c.getColumnIndex(MyDbHandler.COLUMN_SESSION_PATH));
list.add(noteTitle);
c.moveToNext();
}
}
c.close();
db.close();
return list;
I think you should not save notes that you don't want to use (e.g. null or "0"), so instead of checking here, I would check in the addNote method.
For the list initialization you have:
MyDbHandler dbh = new MyDbHandler(this);
ArrayList listData = dbh.getNotesList(id)
if (listData != null && listData.length != 0){
title = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listData.setAdapter(title);
}
I didn't test the code, but I hope it helps you. Good luck!

Categories

Resources