package com.example.smsinterceptor;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver smsReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for SMS permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 1);
}
// Register SMS receiver
smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[]) extras.get("pdus");
for (Object pdu : pdus) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdu);
String messageBody = sms.getMessageBody();
// Add your logic to intercept and process SMS messages
if (messageBody.contains("KEYWORD")) {
// Block this message
abortBroadcast();
Toast.makeText(context, "SMS Blocked", Toast.LENGTH_SHORT).show();
}
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (smsReceiver != null) {
unregisterReceiver(smsReceiver);
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = messages[i].getOriginatingAddress();
String messageBody = messages[i].getMessageBody();
// Add your logic here to check and intercept the sms
if (sender.equals("1234567890") && messageBody.contains("keyword")) {
abortBroadcast(); // Block the SMS
Toast.makeText(context, "SMS from blocked sender intercepted", Toast.LENGTH_SHORT).show();
} else {
// Forward the SMS to default messaging app
}
}
}
}
在AndroidManifest.xml中注册SmsReceiver
接收器:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
请注意,这段代码演示了如何拦截指定发送者和包含特定关键字的短信。您可以根据自己的需求对短信内容进行进一步的处理和拦截。在实际应用中,建议添加权限READ_SMS
和RECEIVE_SMS
。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/150090.html