0

嗨,我希望我的应用程序能够从手机上的联系人导入短信并将其转换为字符串。我想知道这是否有可能以任何方式?我试图寻找它的答案,但似乎没有找到任何答案。希望有人可以帮助我:)

先谢谢了

4

1 回答 1

0
//this class will hold our sms information
public class Sms
{
   public String Id;
   public String Address;
   public String Readstate;
   public String Message;
   public String Time;

   public Sms(string id, string address, string message, string readstate, string time)
   {
      Id = id;
      Address = address;
      Message = message;
      Readstate = readstate;
      Time = time;
   }
}

此功能可以检索用户手机文件夹中的所有 SMS 消息。

这些文件夹只是您在处理 SMS 消息时所期望的基本文件夹(“收件箱”、“已发送”等)

//gets all sms messages in a specific folder in the user's sms messages
public List<Sms> getAllSms(String folderName) 
{
    //initiate a new ArrayList to put our messages in
    //ArrayLists are basically arrays on steroids (this is basic Java stuff)
    List<Sms> lstSms = new ArrayList<Sms>();

    //The SMS object is somewhere in the Android SDK.
    //your IDE should be able to resolve where to find it for you.
    Sms objSms = new Sms();

    //find the SMS messages on the phone in the directory we want
    //using android's content resolver
    Uri message = Uri.parse("content://sms/"+folderName);
    ContentResolver cr = mActivity.getContentResolver();

    //initiate a Cursor object that will help us iterate through the result set
    Cursor c = cr.query(message, null, null, null, null);
    mActivity.startManagingCursor(c);
    int totalSMS = c.getCount();

    //if we can find a message in this result set:
    if (c.moveToFirst()) {
        //iterate through all the messages in our result set
        for (int i = 0; i < totalSMS; i++) {
            //retrieve the contents of this message and put them in "our" Sms object
            objSms = new Sms(
              c.getString(c.getColumnIndexOrThrow("_id")), //retrieve Id, crash if Id cannot be found
              c.getString(c.getColumnIndexOrThrow("address")), //retreive address, crash if it cannot be found
              c.getString(c.getColumnIndexOrThrow("body")), //retreive message content,  crash if it cannot be found
              c.getString(c.getColumnIndex("read")), //retreive whether message is read or not
              c.getString(c.getColumnIndexOrThrow("date")) //retreive message date, crash if it cannot be found
            );
            lstSms.add(objSms);
            c.moveToNext();
        }
    }
    //optionally, you can uncomment the following code to have error handling
    //for empty sms folders:
    // else {
    // throw new RuntimeException("You have no SMS in " + folderName);
    // }

    //close the cursor and free up resources
    c.close();

    //return the sms files we found in the directory
    return lstSms;
}

然后,您可以像这样检索消息:

    List<Sms> inboxMessages = getAllSms("inbox"); // Get all sms from inbox
    List<Sms> sentMessages = getAllSms("sent"); // Get all sms from sent
于 2013-05-06T12:14:21.700 回答