2015-04-03 61 views
0

我想讓消息進入一個字符串並拆分消息。但是我無法得到由「body」表示的消息,以字符串形式表示。請告訴我,如果您知道如何將郵件正文放入字符串中。這是我的示例代碼,如何讓短信正文進行短信跟蹤?

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fulldetail); 

    lst=(ListView)findViewById(R.id.rldlist1); 

    Uri in = Uri.parse("content://sms/inbox"); 
    ContentResolver cr = getContentResolver(); 
    Cursor c = cr.query(in, new String[]{"_id", "address", "body"}, "address = 'eZ Reload'", null, null); 
    adapter = new SimpleCursorAdapter(this, R.layout.rowsms, c, new String[] 
      {"address", "body"}, new int[]{R.id.lblreloadbody, R.id.lblreloadno}); 

    lst.setAdapter(adapter) 

回答

0

可以使用String body = sms.getMessageBody().toString();看看這個代碼:

 // Get the SMS map from Intent 
     Bundle extras = intent.getExtras(); 

     String messages = ""; 

     if (extras != null) 
     { 
      // Get received SMS array 
      Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME); 

      // Get ContentResolver object for pushing encrypted SMS to the incoming folder 
      ContentResolver contentResolver = context.getContentResolver(); 

      for (int i = 0; i < smsExtra.length; ++i) 
      { 
       SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 

       String body = sms.getMessageBody().toString(); 
       String address = sms.getOriginatingAddress(); 

       messages += "SMS from " + address + " :\n";      
       messages += body + "\n"; 

       // Here you can add any your code to work with incoming SMS 
       // I added encrypting of all received SMS 

       putSmsToDatabase(contentResolver, sms); 
      } 

      // Display SMS message 
      Toast.makeText(context, messages, Toast.LENGTH_SHORT).show(); 
     } 

更多詳情:

http://www.apriorit.com/dev-blog/227-handle-sms-on-android

How to read the message content of a new in coming message in android?