[java] view plain copy
- public class CallReceiver extends BroadcastReceiver {
- //接收到广播时就会调用
- @Override
- public void onReceive(Context context, Intent intent) {
- //添加IP线路
- //在打电话广播中,会携带拨打的电话的号码,通过以下代码获取到
- String number = getResultData();
- if(number.startsWith("0")){
- SharedPreferences sp = context.getSharedPreferences("ip", Context.MODE_PRIVATE);
- String ipNumber = sp.getString("ipNumber", "");
- //把IP线路号码添加至用户拨打号码的前面
- number = ipNumber + number;
- //把新的号码重新放入广播中
- setResultData(number);
- abortBroadcast();
- }
- }
- }
短信拦截:
[java] view plain copy
- public class SmsReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- //拿到短信的信息
- //短信内容封装在intent中
- Bundle bundle = intent.getExtras();
- //以pdus为键,取出一个object数组,数组中的每一个元素,都是一条短信
- Object[] objects = (Object[]) bundle.get("pdus");
- //拿到广播中的所有短信
- for (Object object : objects) {
- //通过pdu来构造短信
- SmsMessage sms = SmsMessage.createFromPdu((byte[])object);
- if(sms.getOriginatingAddress().equals("138438")){
- //阻止其他广播接收者收到这条广播
- abortBroadcast();
- // SmsManager.getDefault().sendTextMessage(sms.getOriginatingAddress(), null, "你是个好人", null, null);
- }
- // System.out.println(sms.getMessageBody());
- }
- }
- }
转载:http://blog.csdn.net/chaoyu168/article/details/51025730
时间: 2024-12-03 23:10:28