2013년 4월 10일 수요일

[Android] SMS 목록 가져오기

안드로이드에서 SMS 목록을 가져오는 방법을 소개합니다. 이 방법 또한 전화번호부, 통화내역 가져오는 방법과 유사합니다. AndroidManifest.xml 에 퍼미션 추가


SMSList.java
    public static final String MESSAGE_TYPE_INBOX = "1";
    public static final String MESSAGE_TYPE_SENT = "2";
    public static final String MESSAGE_TYPE_CONVERSATIONS = "3";
    public static final String MESSAGE_TYPE_NEW = "new";

    public void SMSList() {
        try {
            // Retrieve All SMS
            /*
                Inbox = "content://sms/inbox"
                Failed = "content://sms/failed" 
                Queued = "content://sms/queued" 
                Sent = "content://sms/sent" 
                Draft = "content://sms/draft"
                Outbox = "content://sms/outbox"
                Undelivered = "content://sms/undelivered"
                All = "content://sms/all"
                Conversations = "content://sms/conversations"

                addressCol= mCurSms.getColumnIndex("address"); 
                personCol= mCurSms.getColumnIndex("person"); 
                dateCol = mCurSms.getColumnIndex("date"); 
                protocolCol= mCurSms.getColumnIndex("protocol"); 
                readCol = mCurSms.getColumnIndex("read"); 
                statusCol = mCurSms.getColumnIndex("status"); 
                typeCol = mCurSms.getColumnIndex("type"); 
                subjectCol = mCurSms.getColumnIndex("subject"); 
                bodyCol = mCurSms.getColumnIndex("body");
             */
            Uri allMessage = Uri.parse("content://sms/");  
            cur = this.getContentResolver().query(allMessage, null, null, null, null);
            count = cur.getCount();
            Logger.i( TAG , "SMS count = " + count);
            String row = "";
            String msg = "";
            String date = "";
            String protocol = "";
            while (cur.moveToNext()) {
                row = cur.getString(cur.getColumnIndex("address"));
                msg = cur.getString(cur.getColumnIndex("body"));
                date = cur.getString(cur.getColumnIndex("date"));
                protocol = cur.getString(cur.getColumnIndex("protocol"));
                // Logger.d( TAG , "SMS PROTOCOL = " + protocol);  
                
                String type = "";
                if (protocol == MESSAGE_TYPE_SENT) type = "sent";
                else if (protocol == MESSAGE_TYPE_INBOX) type = "receive";
                else if (protocol == MESSAGE_TYPE_CONVERSATIONS) type = "conversations"; 
                else if (protocol == null) type = "send"; 

                Logger.i( TAG , "SMS Phone: " + row + " / Mesg: " + msg + " / Type: " + type + " / Date: " + date);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void SMSDelete() {
        Uri deleteUri = Uri.parse("content://sms");
        int count = 0;
        Cursor c = this.getContentResolver().query(deleteUri, null, null,
                null, null);
        while (c.moveToNext()) {
            try {
                // Delete the SMS
                String pid = c.getString(0);
                // Get id;
                String uri = "content://sms/" + pid;
                // count = this.getContentResolver().delete(Uri.parse(uri),null, null);
            } catch (Exception e) {
            }
        }
    }
참고사이트) Mobdev Wiki
Android - Querying the SMS ContentProvider?
youropensource
Delete SMS in Android 1.5
출처 : http://www.androes.com/144

댓글 없음:

댓글 쓰기

문의하기

이름

이메일 *

메시지 *