2013-05-06 32 views
0

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

感谢:d

+0

你是什么意思的“一条消息”? 短信?彩信?特定于应用的事件消息? – 2013-05-06 12:10:40

+0

来自手机上的联系人的短信 – 2013-05-06 12:12:45

+0

我可以看到为什么你会问这个问题,因为它依赖于使用非文档内容提供者('content:// sms /'),但是有很多例子可以说明如何要做到这一点 – 2013-05-06 12:19:00

回答

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; 
    } 
} 

该功能可以retreive用户的手机上的文件夹中的所有短信。

的文件夹是只是基本的那些与短信打交道时,你会期望(“收件箱”,“已发送”,等等)

//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; 
} 

然后,您可以retreive这样的消息:

List<Sms> inboxMessages = getAllSms("inbox"); // Get all sms from inbox 
    List<Sms> sentMessages = getAllSms("sent"); // Get all sms from sent 
+0

请注意,这些示例中的短信内容提供商没有记录。你的旅费可能会改变。 – 2013-05-06 12:19:25

+0

我不完全确定上面的代码是如何工作的(对于编程有点新颖),你可以试着解释一下吗? :) – 2013-05-06 12:25:16

+0

此外,它看起来像它会发现所有的短信,但我正在寻找一种方法,让用户选择他/她想要导入和使用在APP中的短信。 – 2013-05-06 12:27:25

相关问题