Sainik
Sainik

Reputation: 165

How to list all Call logs from Log list including Sms and email logs also?

I want to access the sms logs also . but I don't find any way , I already accessed the call logs using CallLog.Calls ,it's unable to list sms logs . below code is listing only call logs , but not all logs even sms logs are not listed (sms logs means logs created because received and sent sms) . please help me to find the way to retrieve the sms logs . please ans if possible sir .

Cursor cursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null,
            null, Calls.DATE + " DESC");
    // cursor is inited
    cursor.moveToFirst();

    String name;
    String number;
    int type;
    do {

        name = "";
        number = "";
        type = -1;
        try {
            number = cursor.getString(cursor
                    .getColumnIndex(CallLog.Calls.NUMBER));
            type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
            try {
                name = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.CACHED_NAME));

            } catch (Exception e) {
            } finally {
                if (name == null || name.equals("")) {
                    name = "UNKNOWN";
                }
            }
            Log.e("My App", name + " : "+number + " : "+type);
        } catch (Exception e) {
            Log.e("My App", "Error in creation");
        }
    } while (cursor.moveToNext());

Upvotes: 1

Views: 4171

Answers (3)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

this is just suggestion. you get better answer rather then this...

see if you want to get sms log then use below code.. and you already get call log.. so if you want to mix call and sms log in one list then you must have to do that by date..

in call log you get date & time in sms also you get date & time

sms log code...

put this in method and use your own way..

CharSequence contentTitle = getString(R.string.app_name);
            final ProgressDialog progDailog = ProgressDialog.show(
                    All_logs_tab.this, contentTitle, "Please wait...", true);
            final Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                      /* finish sms load */
                }
            };

            new Thread() {
                public void run() {
                    try {

                        Uri myMessage = Uri.parse("content://sms/");
                        ContentResolver cr = con.getContentResolver();
                        Cursor c = cr.query(myMessage, new String[] { "_id",
                                "address", "date", "body", "read" }, null,
                                null, null);

                        startManagingCursor(c);
                        getSmsLogs(c, con);

                    } catch (Exception e) {
                    }
                    handler.sendEmptyMessage(0);
                    progDailog.dismiss();
                }
            }.start();

................................................. i added all sms details in array list

     ArrayList<String> sms_id = new ArrayList<String>();
     ArrayList<String> sms_num = new ArrayList<String>();
     ArrayList<String> sms_Name = new ArrayList<String>();
     ArrayList<String> sms_dt = new ArrayList<String>();
     ArrayList<String> sms_body = new ArrayList<String>();

........................................................

public void getSmsLogs(Cursor c, Context con) {

        if (sms_num.size() > 0) {
            sms_id.clear();
            sms_num.clear();
            sms_Name.clear();
            sms_body.clear();
            sms_dt.clear();
        }

        try {

            if (c.moveToFirst()) {
                do {



                    if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
                        c.moveToNext();
                        continue;
                    }

                    String _id = c.getString(c.getColumnIndexOrThrow("_id"))
                            .toString();

                    String Number = c.getString(
                            c.getColumnIndexOrThrow("address")).toString();
                    String dat = c.getString(c.getColumnIndexOrThrow("date"))
                            .toString();

                    String as = (String) get_dt(dat, "dd/MM/yyyy, hh.mma");
                    String Body = c.getString(c.getColumnIndexOrThrow("body"))
                            .toString();
                    String name = getContactDisplayNameByNumber("" + Number,
                            con);

                    if (name.length() <= 0 || name.length() == 1) {
                        name = "no name";
                    }
                    sms_id.add(_id);
                    sms_num.add(Number);
                    sms_Name.add("" + name);
                    sms_body.add(Body);
                    sms_dt.add(as);

                } while (c.moveToNext());
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Upvotes: 2

AAnkit
AAnkit

Reputation: 27549

use this link Android call logs to retrieve call logs. for sms do the same by using SMS_URI="content://sms" , but it is not recommended as it is not public URi.

Upvotes: 0

jtt
jtt

Reputation: 13541

You need the URI to the SMS messages's table this will retrieve you the data. It can be found in the base source code, however; it is not recommended by Google to do this.

Upvotes: 0

Related Questions