用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

在QQ通讯录之前拦截短信(android)

2013-01-23 作者: 云代码会员举报

[android]代码库

// 监听注册
	public void register(Handler handler, IProcessObser processObser)
	{
		mContext.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, mSmsObserver);
		
		IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
		filter.setPriority(Integer.MAX_VALUE);
		mContext.registerReceiver(mSmsReceive, filter);
		
		mSmsHandler.addProcessObser(handler, processObser);
	}
	
	// 反注册
	public void unRegister()
	{
		mContext.getContentResolver().unregisterContentObserver(mSmsObserver);
		mContext.unregisterReceiver(mSmsReceive);
		mSmsHandler.removeProcessObser();
	}
	
	// 销毁工作线程
	public void destory()
	{
		mThreadExitFlag = true;
		synchronized (mWorkThread) {
			mWorkThread.notify();
		}
	}
	

	class SmsObserver extends ContentObserver
    {

		public SmsObserver(Handler handler) {
			super(handler);
			// TODO Auto-generated constructor stub		
		}
	
		@Override
		public void onChange(boolean selfChange) {
			// TODO Auto-generated method stub
			super.onChange(selfChange);				
			
			// onChange的执行是在UI线程里做的,所以不能直接去做遍历数据库的耗时操作
			synchronized (mWorkThread) {
				mWorkThread.notify();				// 唤醒工作线程
			}
			
			
		}
	}
	
	class WorkThread extends Thread
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub

			while(true)
			{
				synchronized (this) {
					
					try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					if (mThreadExitFlag == true)
					{
						break;
					}
		
				}
				
				try {
					mSmsHandler.doWork();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			}
			
			
		}
	
		
	}
	
	
	public class SMSReceive extends BroadcastReceiver {

		private final static String TAG = "SMSReceive";

		
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			
			Bundle bundle = intent.getExtras();
			Object messages[] = (Object[]) bundle.get("pdus");
			SmsMessage smsMessage[] = new SmsMessage[messages.length];

			SmsInfo smsInfo = new SmsInfo();
			
			StringBuffer sBuffer = new StringBuffer();
			for(int n = 0; n < messages.length; n++)
			{
				smsMessage[n]= SmsMessage.createFromPdu((byte[]) messages[n]); 
				sBuffer.append(smsMessage[n].getMessageBody());
				
				smsInfo.address = smsMessage[n].getOriginatingAddress();
			}
			
			smsInfo.body = sBuffer.toString();
			smsInfo.date = String.valueOf(System.currentTimeMillis());
			
		
			if (SmsFilter.isFilter(smsInfo))				// 过滤短信并处理
			{
				abortBroadcast();	
				
				mSmsHandler.processSMSInfo(smsInfo);
			}
			

			
		}

		
			
	}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...