2011-08-31 36 views
1

我開發了一個應用程序,它監聽所有傳入的SMS(使用廣播接收器),但我想讓我的應用程序在收聽特定SMS(Ex 。「LOCATIONRECQEST」來自手機號碼 - 98765 ...)。 所以,基本上我想知道的是如何檢查SMS的內容(並相應地執行一些任務)。 另外我想知道發送短信的號碼。 任何幫助等待。 謝謝。如何檢查通過Android中的廣播接收器收到的特定SMS

回答

0

要閱讀短信內容,可以使用ContentResolver的

// Uri of the SMS inbox 
Uri uri = Uri.parse("content://sms/inbox"); 

// Query 
Cursor c = getContentResolver().query(uri, null, null, null, null); 

if (null != c && c.moveToFirst()) { 

    // For all SMS 
    // do { 

    String date = c.getString(c.getColumnIndex("date")); // Date (Timestamp) 
    String adress = c.getString(c.getColumnIndex("address")); // Phone number 
    String body = c.getString(c.getColumnIndex("body")); // Message 

    // For all SMS 
    // }while(c.moveToNext()); 

} 

中查找的字符串在您的短信內容:

String body = "this is the text of the sms - code 25D45 - ..."; 
String stringToFind = "25D45"; 

// return the position of the "stringToFind" 
if(body.indexOf(stringToFind) != -1) { 

    // your action here 

} 
+0

我正在通過下面的代碼收到的消息,但我想用一些字符串comapare收到的消息。我能怎麼做? –

+0

public void onReceive(Context context,Intent intent){Bundle bundle = intent.getExtras(); Object messages [] =(Object [])bundle.get(「pdus」); SmsMessage smsMessage [] = new SmsMessage [messages.length];對於(int n = 0; n

+0

我在上面添加了一個代碼 –