2011-08-31 38 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

我在上面添加了一个代码 –