这很容易通过在您的清单中写入广播接收器来完成:
编辑:似乎只适用于 SMS_RECEIVED 看到这个线程
<receiver android:name=".SMSReceiver" android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
和许可:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
然后像这样创建接收器:
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//do something with the received sms
}else if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
//do something with the sended sms
}
}
}
处理传入的短信可能如下所示:
Bundle extras = intent.getExtras();
Object[] pdus = (Object[]) extras.get("pdus");
for (Object pdu : pdus) {
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);
String origin = msg.getOriginatingAddress();
String body = msg.getMessageBody();
....
}
如果您想防止在 commen InBox 中推送短信,您可以通过以下方式实现:
abortBroadcast();