黑名单电话短信拦截实现

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();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值