在Android的短信座 [英] Block SMS in android
问题描述
我开发,我想阻止某些特定numbers.For测试我做短信从这个数字,我在if条件的短信应用,但这code未阻止该号码的短信。我尝试最好的,但我以前不解决。任何一个帮助me.Here是我的code结果
Sms.java
公共类短信扩展广播接收器{
最后SmsManager短信= SmsManager.getDefault(); @覆盖
公共无效的onReceive(上下文的背景下,意图意图){ //获取地图从意图扩展数据。
最后的捆绑包= intent.getExtras(); 尝试{ 如果(捆绑!= NULL){ 最终目标[] = pdusObj(对象[])bundle.get(的PDU); 的for(int i = 0; I< pdusObj.length;我++){ SmsMessage currentMessage = SmsMessage
((字节[])pdusObj [I]).createFromPdu;
字符串phoneNumber的= currentMessage
.getDisplayOriginatingAddress(); 字符串senderNum = phoneNumber的;
字符串消息= currentMessage.getDisplayMessageBody(); Log.i(SmsReceiver,senderNum:+ senderNum
+的消息:+消息);
//显示警报
INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,senderNum:
+ senderNum +,留言:+消息,持续时间);
toast.show();
如果(senderNum ==+ 923215619915){
Toast.makeText(背景下,你如果条件R IN,Toast.LENGTH_LONG).show();
abortBroadcast();
} } //结束for循环
} //包是空 }赶上(例外五){
Log.e(SmsReceiver,异常smsReceiver+ E); }
}
}
的Manifest.xml
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.tesingworkspace
安卓版code =1
机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =7
机器人:targetSdkVersion =19
/>
<使用许可权的android:NAME =android.permission.SEND_SMS>
< /使用许可权>
<使用许可权的android:NAME =android.permission.READ_CONTACTS/>
<使用许可权的android:NAME =android.permission.RECEIVE_SMS>
< /使用许可权>
<使用许可权的android:NAME =android.permission.READ_SMS/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=com.example.tesingworkspace.BroadCastSms
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <接收机器人:名字=com.example.tesingworkspace.Sms>
&所述;意图滤光器>
<作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
&所述; /意图滤光器>
< /接收器> < /用途> < /清单>
您需要做的是调试code的最好的事情,你有难$ C $光盘格式的电话号码可能会或可能不会发生,确保在出现什么格式接收机的电话号码?当我工作了它我会发送消息通过 0333xxx-XXXX
号码和我收到的 + 92xxx
。但它不能确定所有的,不同的电信公司可能会使用不同的格式,你应该使用
如果(number.equals(+ 92333xxx-XXXXX))
或更好的使用包含
,即实际上使其更适合匹配号码,并移除格式错误的可能性
如果(number.contains(333xxx-XXXXX)){
//你的code中止的消息
}
希望这有助于
i developing an application where i want to block SMS of some specific numbers.For testing i do SMS from that number that i have in IF CONDITION but this code is not blocking that number SMS. i try best but did't resolved. any one help me.Here is my code
Sms.java
public class Sms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
@Override
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();
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();
if(senderNum=="+923215619915"){
Toast.makeText(context, "You r in if condition", Toast.LENGTH_LONG).show();
abortBroadcast();
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tesingworkspace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="19"
/>
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tesingworkspace.BroadCastSms"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.tesingworkspace.Sms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
The best thing you need to do is debug your code, you have hard coded the phone number in a format that may or may not occur, make sure in what format your receiver phone number appears? When I was working over it I was sending message over 0333xxx-xxxx
number and I was receiving as +92xxx
. But it's not sure for all, different telecommunication companies may use different format, for that you should use
if(number.equals("+92333xxx-xxxxx"))
or better use a contains
, that would actually make it more appropriate to match the number and remove the possibility of format error
if(number.contains("333xxx-xxxxx")){
// Your code to abort message
}
Hope this helps
这篇关于在Android的短信座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!