Android简易短信拦截器

时间: 2012-06-23 / 分类: 开发实例 / 浏览次数: 1,197 views / 3个评论 发表评论

自己写的用android短信拦截,实现输入手机号拦截该手机的短信的功能,可以作为参考

软件下载地址

下面我来介绍一下该程序实现:

1.首先是主界面:

主界面代码:

package com.huangcheng.smscut;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmsCut extends Activity {
	private EditText sms_num_edit;
	private Button yes_btn;
	private Button no_btn;

	private SmsRecevier recevier=null;
	private boolean isregiset = false;

	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sms_num_edit = (EditText) findViewById(R.id.sms_number_edit);
		yes_btn = (Button) findViewById(R.id.yes_btn);
		no_btn = (Button) findViewById(R.id.no_btn);
		yes_btn.setEnabled(true);
		no_btn.setEnabled(false);
		yes_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				regiset();
				yes_btn.setEnabled(false);
				no_btn.setEnabled(true);
			}
		});
		no_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				unregiset();
				SmsCut.this.finish();
			}
		});
	}
	public void regiset() {
		IntentFilter filter = new IntentFilter(ACTION);
		filter.setPriority(1000);// 设置优先级最大
		recevier = new SmsRecevier(sms_num_edit.getText().toString().trim());
		registerReceiver(recevier, filter);
		isregiset = true;
		Toast.makeText(this, "开始拦截", 0).show();
	}
	public void unregiset() {
		if (recevier != null && isregiset) {
			unregisterReceiver(recevier);
			isregiset = false;
			Toast.makeText(this, "停止拦截,关闭程序", 0).show();
		} else
			Toast.makeText(this, "尚未设置,关闭程序", 0).show();
	}
	protected void onDestroy() {
		super.onDestroy();
		if (recevier != null && isregiset) {
			unregisterReceiver(recevier);
			isregiset = false;
			Toast.makeText(this, "停止拦截,关闭程序", 0).show();
		}
	}
}

主要实现了:注册broadcastReceiver实现监听短信信息,就是

android.provider.Telephony.SMS_RECEIVED

BroadcastRecevier代码:

package com.huangcheng.smscut;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsRecevier extends BroadcastReceiver {
	private String num;

	public SmsRecevier(String number) {
		Log.v("TAG", "SmsRecevier create");
		num=number;
	}

	// 接受短信
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v("TAG", "SmsRecevier onReceive");
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		if (pdus != null && pdus.length > 0) {
			SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < pdus.length; i++) {
				byte[] pdu = (byte[]) pdus[i];
				messages[i] = SmsMessage.createFromPdu(pdu);
			}
			for (SmsMessage message : messages) {
				String content = message.getMessageBody();// 得到短信内容
				String sender = message.getOriginatingAddress();// 得到发信息的号码
				if (sender.equals(num)) {
					abortBroadcast();// 中止发送
					Log.e("TAG", "此号码为黑名单号码,已拦截!");
				}
				/*
				 *
				 * 回复信息需要用到
				Date date = new Date(message.getTimestampMillis());
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String sendContent = format.format(date) + ":" + sender + "--"
						+ content;
				SmsManager smsManager = SmsManager.getDefault();// 发信息时需要的
				smsManager.sendTextMessage("", null, sendContent, null, null);// 转发给
				Log.v("TAG", sendContent);
				*/
			}
		}
	}
}

该recevier实现了截断短信发送broadcastreceiver,可以转发短信并截断该短信发送。

对了,记得要设置BroadCastRecevier优先级,这样才能首先接听到,还有记得要申明权限:

	<uses-permission android:name="android.permission.RECEIVE_SMS" /><!--
		接收短信权限 -->
	<uses-permission android:name="android.permission.SEND_SMS" /><!--
		发送短信权限 -->
	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

好了 到此结束

 



.

*******************************************************************

上面是广告! 真的是广告!! 真的真的啊!!!

-----------------------------------------------------Copyright © 2011-2012--浙ICP备12011997---------------------------- ---------------------- ------------------------

无觅相关文章插件,快速提升流量