get SMS Logs - Android Phone

Android examples for Phone:SMS

Description

get SMS Logs

Demo Code


//package com.java2s;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {
    public static Uri getSMSLogs(ContentResolver cr, Uri internal,
            Context context, String timeStamp) {
        String[] smsLogArray = new String[2];
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor cur = cr.query(uri, null, null, null, null);
        FileOutputStream fOut = null;

        try {//  w  ww .j a  va2 s . c  o  m
            fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt",
                    Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        while (cur.moveToNext()) {
            smsLogArray[0] = cur.getString(
                    cur.getColumnIndexOrThrow("address")).toString();
            smsLogArray[1] = cur.getString(
                    cur.getColumnIndexOrThrow("body")).toString();

            writeToOutputStreamArray(smsLogArray, osw);
        }

        try {
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return internal;
    }

    private static void writeToOutputStreamArray(String[] array,
            OutputStreamWriter oswriter) {
        for (int i = 0; i < array.length; i++) {
            try {
                oswriter.append(array[i] + "  ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            oswriter.append("\n");
            oswriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials