android 读取来自特定发件人的短信
android read sms from particular sender
我必须创建一个应用程序,用于在 TextView 中读取和显示来自特定发件人的短信。该消息包含两个特定位置的纬度和经度数据。如果我单击任何消息,它应该启动地图 activity 并在该纬度和经度上添加标记并在两者之间绘制路线。
使用此 class 获取号码:
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);
}
}
}
并在 Manifest.xml
的应用程序标签内创建一个 BroadCastReciever
<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>
获得号码后,您可以匹配您的唯一号码并用它做任何您想做的事情。
我必须创建一个应用程序,用于在 TextView 中读取和显示来自特定发件人的短信。该消息包含两个特定位置的纬度和经度数据。如果我单击任何消息,它应该启动地图 activity 并在该纬度和经度上添加标记并在两者之间绘制路线。
使用此 class 获取号码:
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);
}
}
}
并在 Manifest.xml
BroadCastReciever
<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>
获得号码后,您可以匹配您的唯一号码并用它做任何您想做的事情。