我已阅读大部分有关阻止短信进入收件箱的问题。没有什么对我有用。所以,我现在有两个应用程序,一个是发送,另一个是接收器。接收者需要检查消息的前四个字符,如果它来自Sender应用程序。然后,阻止它进入收件箱。 他们说我必须提高应用程序的优先级。我做了,这是我的清单
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
而且,为了清楚起见,这是我检查消息的活动。
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
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;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
H = currentMessage.getMessageBody();
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
if(i==0)
H = currentMessage.getMessageBody();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
check_message = message_checked(H) ;
//---display the new SMS message---
if(check_message)
{
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.putExtra("msgContent", message);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
}
else
System.exit(0);
}
public boolean message_checked(String the_message)
{
String Code = new String("*%#&");
String four_char = "" ;
for(int i = 0 ; i<4 ; i++)
{
four_char += the_message.charAt(i) ;
}
if(four_char.equals(Code))
{
this.abortBroadcast();
return true;
}
else
return false ;
}
我做错了什么吗?如果我这样做了,那就是错误。我仍然收到收件箱中的短信虽然应用程序正在按我的意愿工作。
答案 0 :(得分:0)
这就是我过去的做法,抱歉我的模拟器现在不能正常工作所以我无法检查。希望它有所帮助。
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")){
// Check if the SMS matches our SOS message
SmsMessage[] messages = getMessagesFromIntent(intent);
if(messages != null){
for(int i = 0; i < messages.length; i++){
SmsMessage message = messages[i];
if(matchesMessage(context,message.getDisplayMessageBody())){
Log.i(LOGNAME, "Received Test String! Abort Broadcast");
//put intent call or preferred code here
this.abortBroadcast();
break;
}
}
}
}
}
private boolean matchesMessage(Context context, String message){
SharedPreferences preferences = context.getSharedPreferences(MainSetting.PREFERENCES, Context.MODE_PRIVATE);
String testMessage = preferences.getString(MainSetting.PREFERENCES_TEST, MainSetting.PREFERENCES_STRING_DEFAULT); //this is a string stored in shared preferences
return testMessage.equalsIgnoreCase(message); //ignorecase so that it is not case sensitive
}
private SmsMessage[] getMessagesFromIntent(Intent intent) {
SmsMessage retMsgs[] = null;
Bundle bdl = intent.getExtras();
try {
Object pdus[] = (Object[]) bdl.get("pdus");
retMsgs = new SmsMessage[pdus.length];
for (int n = 0; n < pdus.length; n++) {
byte[] byteData = (byte[]) pdus[n];
retMsgs[n] = SmsMessage.createFromPdu(byteData);
}
} catch (Exception e) {
Log.e(LOGNAME, "fail", e);
}
return retMsgs;
}
}