如何在列表中显示所有框内消息?

时间:2012-01-10 11:33:43

标签: android

我是新手。

我正在开发一个与发送和接收短信有关的应用程序。

我想在列表中显示所有接收的短信。

如果有人知道请帮帮我

提前感谢。

2 个答案:

答案 0 :(得分:2)

试试这个:

/* location of sms inbox*/

public static final String CONTENT = "content://sms/inbox";
/*location of sms on sim card */
public static final String SIMCONTENT = "content://sms/sim";
/*name of address field*/
public static final String ADDRESS = "address";
/*name of body field*/
public static final String BODY = "body";

/*cursor incoming sms*/
private static Cursor inboxCursor;
/*cursor sms on sim card*/
private static Cursor simCursor;

   initCursors(Context context){
        inboxCursor = context.getContentResolver()
                        .query(Uri.parse(CONTENT), null, null, null, null);
        simCursor = context.getContentResolver()
                        .query(Uri.parse(SIMCONTENT), null, null, null, null);  
}

 /**
  * Reading SMS from telephone memory.
  * @return a Map object where key - telephone number, value - a  List 
  * consisting of messages.
  */
public Map<String, List<String>> readTelephoneSms() {
    Map<String, List<String>> smsData = new HashMap<String, List<String>>();
    while (inboxCursor.moveToNext()) { 
        if(smsData.containsKey(inboxCursor.getString(inboxCursor.getColumnIndex(ADDRESS)))){
            smsData.get(inboxCursor.getString(inboxCursor.getColumnIndex(ADDRESS)))
                    .add(inboxCursor.getString(inboxCursor.getColumnIndexOrThrow(BODY)));
        }else{
            List<String> data = new ArrayList<String>();
            data.add(inboxCursor.getString(inboxCursor.getColumnIndexOrThrow(BODY)));
            smsData.put(inboxCursor.getString(inboxCursor.getColumnIndex(ADDRESS)), data);
        }
    }

    return smsData;
}

/**
  * Reading SMS from Sim card memory.
  * @return a Map object where key - telephone number, value - a  List
  * consisting of messages.
  */
public Map<String, List<String>> readSimSms() {
    Map<String, List<String>> smsData = new HashMap<String, List<String>>();
  while (simCursor.moveToNext()) { 
    if(smsData.containsKey(simCursor.getString(simCursor.getColumnIndex(ADDRESS)))){
        smsData.get(simCursor.getString(simCursor.getColumnIndex(ADDRESS)))
                .add(simCursor.getString(simCursor.getColumnIndexOrThrow(BODY)));
    }else{
        List<String> data = new ArrayList<String>();
        data.add(simCursor.getString(simCursor.getColumnIndexOrThrow(BODY)));
        smsData.put(simCursor.getString(simCursor.getColumnIndex(ADDRESS)), data);
    }           
}
    return smsData;
}

答案 1 :(得分:2)

它有一些很长的东西......

我刚刚为您提供了这些链接,满足您的需求。只要看一下,让我知道它是否对你有所帮助..

FrontPage/Tutorials/SMS Messaging

android-smspopup

另请参阅此问题How can I read SMS messages from the inbox programmatically in Android?