android从特定发件人读取短信

时间:2015-05-06 09:09:50

标签: android dictionary sms

我必须创建一个应用程序,它在TextView中读取和显示来自特定发件人的短信。该消息包含两个特定位置的纬度和经度数据。如果我点击任何消息,它应该启动一个地图活动,并在该纬度和经度上添加标记,并在两者之间绘制路线。

1 个答案:

答案 0 :(得分:0)

使用此类获取数字:

public class IncomingSms extends BroadcastReceiver {
public static String SERVER_SUCCESS_CODE = "";

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

try {

    if (bundle != null) {

        final Object[] pdusObj = (Object[]) bundle.get("pdus");

        for (int i = 0; i < pdusObj.length; i++) {

            SmsMessage currentMessage = SmsMessage
                    .createFromPdu((byte[]) pdusObj[i]);
            String phoneNumber = currentMessage
                    .getDisplayOriginatingAddress();

            String senderNum = phoneNumber;
            String message = currentMessage.getDisplayMessageBody();
            String[] split_one = message.split(":");
            String  ss = split_one[1].substring(0, 4);
            if(ss.length()==4)
            {
                Constants.SERVER_SUCCESS_CODE=ss;
            }

            Log.i("SmsReceiver", "senderNum: " + senderNum
                    + "; message: " + message);

            // Show Alert
            /*int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "senderNum: "
                    + senderNum + ", message: " + message, duration);
            toast.show();*/

        } 
    } 

} 
catch (Exception e) {
    Log.e("SmsReceiver", "Exception smsReceiver" + e);

}
}
}

BroadCastReciever

的应用标记内创建Manifest.xml
<receiver android:name=".IncomingSms" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

并提供一些权限

 <uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>

获得号码后,您可以匹配您的唯一号码,并随心所欲地做任何事情。