1.创建一个包名为 com.android.internal.telephony的 aidl文件
package com.android.internal.telephony;
interface ITelephony {
/**
* Dial a number. This doesn't place the call. It displays
* the Dialer screen.
* @param number the number to be dialed. If null, this
* would display the Dialer screen with no number pre-filled.
*/
void dial(String number);
/**
* Place a call to the specified number.
* @param number the number to be called.
*/
void call(String number);
/**
* End call if there is a call in progress, otherwise does nothing.
*
* @return whether it hung up
*/
boolean endCall();
/**
* Answer the currently-ringing call.
*
* If there's already a current active call, that call will be
* automatically put on hold. If both lines are currently in use, the
* current active call will be ended.
*
* TODO: provide a flag to let the caller specify what policy to use
* if both lines are in use. (The current behavior is hardwired to
* "answer incoming, end ongoing", which is how the CALL button
* is specced to behave.)
*
* TODO: this should be a oneway call (especially since it's called
* directly from the key queue thread).
*/
void answerRingingCall();
}
2、创建服务文件:
/**
* 文件名:SmsDenfenceService.java
* author: jimbo
* 版本信息 : copyright 2014
* 日期:2014-10-17
*
*/
package xhw.security.defence;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.android.internal.telephony.ITelephony;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* @author Administrator
*
*/
public class DenfenceService extends Service {
private TelephonyManager mTelephonyManager = null;
private AudioManager mAudioManager = null;
private SmsManager mSmsManager = null;
private SmsBroadcastReceiver mSmsReceiver = null;
/* (non-Javadoc)
* @see android.app.Service#onCreate()
*/
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
log("RejectPhoneSmsService:onCreate");
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(new XHWPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//拦截短信
mSmsReceiver = new SmsBroadcastReceiver();
IntentFilter smsIntentFilter = new IntentFilter();
smsIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"/*android.provider.Telephony.SMS_RECEIVED_ACTION*/);
smsIntentFilter.setPriority(1000); // 优先级最高
registerReceiver(mSmsReceiver, smsIntentFilter);
}
/* (non-Javadoc)
* @see android.app.Service#onDestroy()
*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
log("RejectPhoneSmsService onDestroy");
unregisterReceiver(mSmsReceiver);
}
// 电话状态监控
private class XHWPhoneStateListener extends PhoneStateListener{
/* (non-Javadoc)
* @see android.telephony.PhoneStateListener#onCallStateChanged(int, java.lang.String)
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
log("onCallStateChanged state="+state + " incomingNumber="+ incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: //idle
break;
case TelephonyManager.CALL_STATE_RINGING: // incoming call
DenfenceService.this.mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);// 静音
ITelephony it = getITelephony();
if(it != null){
try {
it.endCall(); //挂断电话
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //incall
break;
default:
break;
}
}
}
// 反射机制使用 类 TelephonyManager 的 ITelephony getITelephony方法
private ITelephony getITelephony(){
ITelephony it = null;
Class<TelephonyManager> cls = TelephonyManager.class;
try {
/*private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
}*/
Method getITelephonyMethod = cls.getDeclaredMethod("getITelephony");
if(getITelephonyMethod != null){
getITelephonyMethod.setAccessible(true); //取消private属性
it = (ITelephony) getITelephonyMethod.invoke(this.mTelephonyManager);
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return it;
}
/* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
log("RejectPhoneSmsService:onBind");
mTelephonyManager = (TelephonyManager)super.getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(new PhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
return mBinder;
}
private IBinder mBinder = new XHWBinder();
private class XHWBinder extends Binder{
}
private class SmsBroadcastReceiver extends BroadcastReceiver{
private static final boolean DEBUG = false;
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
// debug 打印短信内容
StringBuffer sb = new StringBuffer();
String sender = null;
String content = null;
String sendtime = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
// 通过pdus获得接收到的所有短信消息,获取短信内容;
Object[] pdus = (Object[]) bundle.get("pdus");
// 构建短信对象数组;
SmsMessage[] mges = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
// 获取单条短信内容,以pdu格式存,并生成短信对象;
mges[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage mge : mges) {
sender = mge.getDisplayOriginatingAddress();// 获取短信的发送者
content = mge.getMessageBody();// 获取短信的内容
sb.append("sms comes from:" + sender + "\n");
sb.append("content is:" + content + "\n");
Date date = new Date(mge.getTimestampMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sendtime = format.format(date);// 获取短信发送时间;
sb.append("send time is:" + sendtime + "\n");
}
log(sb.toString());
}
// 屏蔽全部短信
abortBroadcast();
}
}
/**
* @param string
*/
private void log(String string) {
// TODO Auto-generated method stub
Log.d("jimbo", string);
}
}
3、启动服务:
public class StartAppReceiver extends BroadcastReceiver {
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("jimbo", "StartAppReceiver onReceive()");
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Toast.makeText(context, "启动成功,开启防护服务!", Toast.LENGTH_LONG).show();
Log.d("jimbo", "启动成功,开启防护服务!");
//启动拒接电话和短信服务
context.startService(new Intent(context, DenfenceService.class));
}
}
}
4、app需要用到的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- <uses-permission android:name="android.permission.CALL_PRIVILEGED" /> -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />