这当然是可能的顶部。 注册您的接收器在您的清单如下:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
在你的接收机的onReceive方法写:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
//do your stuff
abortBroadcast();
}
}
记住设置权限在清单这样收到短信:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
如果没有正确的权限设置,它将以下面的logcat消息静默失败:
07-13 12:54:13.208: WARN/ActivityManager(201): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to com.mypackage requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)
最后,另一个应用程序可能已经注册了一个比您的优先级更高的接收者,并且因此它在您取消它之前接收广播。所以如果一切都失败了,试试更高优先级的值。
这是可能的,使用您在问题中描述的方法。您是否记得在清单中设置正确的权限?看看你的logcat输出,以确定系统注册传入短信的位置,并找出接收器为什么不拦截它。 – Marmoy 2011-07-13 11:03:19
我已经提交了一个完整的答案,以澄清 – Marmoy 2011-07-13 11:04:30