import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import com.study.mobilesafe.db.dao.BlackNumberDao;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
public class BlackNumberService extends Service {
private SmsReceiver receiver;
private BlackNumberDao dao;
private MyPhoneListener listener;
private TelephonyManager manager;
private class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objs) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) obj);
String sender = message.getOriginatingAddress();
int queryMode = dao.queryMode(sender);
if (queryMode == BlackNumberDao.SMS
|| queryMode == BlackNumberDao.ALL) {
abortBroadcast();
}
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
dao = new BlackNumberDao(getApplicationContext());
receiver = new SmsReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
// 电话状态发生改变调用这个方法
private class MyPhoneListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state,final String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
int queryMode = dao.queryMode(incomingNumber);
if (queryMode == BlackNumberDao.TEL
|| queryMode == BlackNumberDao.ALL) {
endCall();
// 删除通话记录
// 不会立即生成通话记录--使用内容观察者
final Uri uri = Uri.parse("content://call_log/calls");
final ContentResolver resolver = getContentResolver();
resolver.registerContentObserver(uri, true,
new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
resolver.delete(uri, "number=?",
new String[] { incomingNumber });
// 不需要在观察了
resolver.unregisterContentObserver(this);
}
});
}
break;
default:
break;
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
manager.listen(listener, PhoneStateListener.LISTEN_NONE);// 取消监听
// 反注册短信广播接收者
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
}
/**
* 挂断电话
*/
public void endCall() {
Class<?> loadClass = null;
try {
loadClass = BlackNumberService.class.
getClassLoader().loadClass("android.os.ServiceManager");
Method declaredMethod = loadClass.
getDeclaredMethod("getService",String.class);
IBinder invoke = (IBinder) declaredMethod.invoke(null,
Context.TELEPHONY_SERVICE);
ITelephony asInterface = ITelephony.Stub.asInterface(invoke);
asInterface.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
黑名单电话短信拦截实现
最新推荐文章于 2017-07-15 22:11:41 发布