在Android中实现电话和短信拦截器功能,这个是我从网上找的,并非自己实现,使用时需要修改一下代码(需要拦截的号码),测试已经通过,工程結构和实现如下。
2. 工程结构
3. 功能实现
(1) 主界面(main.xml)实现:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > <Button android:id = "@+id/enableCallTransfer" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "设置呼叫转移" /> <Button android:id = "@+id/disableCallTransfer" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "取消呼叫转移" /> </LinearLayout>
(2) 主Activity实现:
package com.focus.manager; import java.lang.reflect.Method; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.media.AudioManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.telephony.SmsMessage; import android.telephony.TelephonyManager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import com.android.internal.telephony.ITelephony; public class CallAndSMSInterceptorActivity extends Activity { //要拦截的号码 private final static String BLOCKED_NUMBER = "15555215556"; private IncomingCallReceiver mIncomingCallReceiver; private ITelephony mITelephony; private AudioManager mAudioManager; //占线时转移,这里13800000000是空号,所以会提示所拨的号码为空号 private final String ENABLE_SERVICE = "tel:**67*13800000000%23"; //占线时转移 private final String DISABLE_SERVICE = "tel:%23%2367%23"; private final static int OP_REGISTER = 100; private final static int OP_CANCEL = 200; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mIncomingCallReceiver = new IncomingCallReceiver(); IntentFilter mIntentFilter = new IntentFilter(); //拦截电话 mIntentFilter.addAction("android.intent.action.PHONE_STATE"); //拦截短信 mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); //注册BroadcastReceiver registerReceiver(mIncomingCallReceiver, mIntentFilter); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //利用反射获取隐藏的endcall方法 TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); try { Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null); getITelephonyMethod.setAccessible(true); mITelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } findViewById(R.id.enableCallTransfer).setOnClickListener(new OnClickListener(){ public void onClick(View view) { //设置呼叫转移 Message message = mHandler.obtainMessage(); message.what = OP_REGISTER; mHandler.dispatchMessage(message); } }); findViewById(R.id.disableCallTransfer).setOnClickListener(new OnClickListener(){ public void onClick(View view) { //取消呼叫转移 Message message = mHandler.obtainMessage(); message.what = OP_CANCEL; mHandler.dispatchMessage(message); } }); } private class IncomingCallReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d("mayingcai", "Action:" + action); if("android.intent.action.PHONE_STATE".equals(action)){//拦截电话 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); Log.i("mayingcai", "State: "+ state); String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.d("mayingcai", "Incomng Number: " + number); if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){//电话正在响铃 if(number.equals(BLOCKED_NUMBER)){//拦截指定的电话号码 //先静音处理 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); Log.d("mayingcai", "Turn Ringtone Silent"); try { //挂断电话 mITelephony.endCall(); } catch (Exception e) { e.printStackTrace(); } //再恢复正常铃声 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } } }else if("android.provider.Telephony.SMS_RECEIVED".equals(action)){//拦截短信 SmsMessage sms = getMessagesFromIntent(intent)[0]; String number = sms.getOriginatingAddress(); Log.d("mayingcai", "Incomng Number: " + number); number = trimSmsNumber("+86", number);//把国家代码去除掉 if(number.equals(BLOCKED_NUMBER)){ abortBroadcast();//这句很重要,中断广播后,其他要接收短信的应用都没法收到短信广播了 } } } } public final static SmsMessage[] getMessagesFromIntent(Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; } public final static String trimSmsNumber(String prefix, String number){ String s = number; if(prefix.length()>0 && number.startsWith(prefix)){ s = number.substring(prefix.length()); } return s; } private Handler mHandler = new Handler() { public void handleMessage(Message response) { int what = response.what; switch(what) { case OP_REGISTER:{ Intent i = new Intent(Intent.ACTION_CALL); i.setData(Uri.parse(ENABLE_SERVICE)); startActivity(i); break; } case OP_CANCEL:{ Intent i = new Intent(Intent.ACTION_CALL); i.setData(Uri.parse(DISABLE_SERVICE)); startActivity(i); break; } } } }; }