本文介绍了任何方式来获得在Android上收到的短信发送者的电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写的发送和接收sucsessfully SMS程序,但我想在的onReceive方法检查接收的电话号码。我怎样才能在Android上收到的短信发送者的电话号码?
我写这篇code,但它dosent工作!请检查并帮助我。
公共类SmsReceiver扩展广播接收器{
公共字符串str =; @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// ---获得通过SMS消息--- 捆绑包= intent.getExtras();
SmsMessage []封邮件= NULL; 如果(捆绑!= NULL){ [对象]的PDU =(对象[])bundle.get(的PDU);
封邮件=新SmsMessage [pdus.length] 的for(int i = 0; I< msgs.length;我++){
封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]); //从特殊号码获取短信===============================
字符串msg_from =封邮件[I] .getOriginatingAddress();
Log.v(msg_from>>中,msg_from);
如果(msg_from.equals(08522215))
{
// ===============================
STR + =短信来自+封邮件[I] .getOriginatingAddress();
STR + =;
STR + =封邮件[I] .getMessageBody()的toString()。
STR + =\\ n;
} }
// ---显示新短信---
// Toast.makeText(背景下,STR,Toast.LENGTH_SHORT).show();
意向行为=新意图(背景下,MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra(信息,STR);
context.startActivity(行为);
} abortBroadcast();
}
}
的manifest.xml
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.sms
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =7
机器人:targetSdkVersion =15/> <使用许可权的android:NAME =android.permission.SEND_SMS>
< /使用许可权>
<使用许可权的android:NAME =android.permission.RECEIVE_SMS>
< /使用许可权><使用许可权的android:NAME =android.permission.READ_SMS/>
<应用机器人:图标=@绘制/ ic_launcher机器人:标签=@字符串/ APP_NAME>
<活动机器人:短信NAME =
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <活动机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME/>
<接收
机器人:名字=com.example.sms.SmsReceiver
类=com.example.sms.SmsReceiver>
<意向过滤器的android:优先=100>
<作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
&所述; /意图滤光器>
< /接收器>
< /用途>
< /清单>
解决方案
在广播接收机写这个
捆绑包= intent.getExtras(); 对象信息[] =(Object []对象)bundle.get(的PDU);
SmsMessage smsMessage [] =新SmsMessage [messages.length] 对于(INT N = 0; N< messages.length; N ++)
{
smsMessage [η] = SmsMessage.createFromPdu((字节[])消息[N]);
}
。字符串receivedMessage = smsMessage [0] .getMessageBody()的toString()与toUpperCase()。
串originatingAddress = smsMessage [0] .getOriginatingAddress();
originatingAddress =(originatingAddress =空&放大器;!&放大器; originatingAddress.length()→3)(originatingAddress.substring(3)):()?;
以上始发的地址实际上是发送号码...
修改
您可能使用
android.telephony.gsm.SmsMessage
这是pcated德$ P $。用这个来代替:
android.telephony.SmsMessage
I wrote send and receive sms program sucsessfully but I want to that checked receiver phone number in onReceive method. how can i get the sender phone number from a received sms in android?I wrote this code but it dosent worked!!! please check it and help me.
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
@Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
//for get sms from special number===============================
String msg_from = msgs[i].getOriginatingAddress();
Log.v("msg_from >>",msg_from);
if(msg_from.equals("08522215"))
{
//===============================
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
// ---display the new SMS message---
// Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name"/>
<receiver
android:name="com.example.sms.SmsReceiver"
class="com.example.sms.SmsReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
解决方案
Write this in the broadcast receiver
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();
String originatingAddress = smsMessage[0].getOriginatingAddress();
originatingAddress = (originatingAddress!=null && originatingAddress.length()>3)?(originatingAddress.substring(3)):("") ;
The Originating address above is actually the sender number...
EDIT
you probably used
android.telephony.gsm.SmsMessage
that is deprecated. Use this instead:
android.telephony.SmsMessage
这篇关于任何方式来获得在Android上收到的短信发送者的电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!