Android短信拦截和电话拦截

MainActivity:

package com.wyl.bctest;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	private final static String ACTION_SMS_RECEIVE = "android.provider.Telephony.SMS_RECEIVED";
	private final static String ACTION_CALL_OUT = "android.intent.action.NEW_OUTGOING_CALL";
	Button btn;
	Button btn2;
	Button btn3;
	Button btn4;
	TextView tv;
	BCReceiver re = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btn01);// 模拟发送广播
		btn2 = (Button) findViewById(R.id.btn02);// 注册广播
		btn3 = (Button) findViewById(R.id.btn03);// 取消注册广播
		btn4 = (Button) findViewById(R.id.btn04);
		MyOnclickListener l = new MyOnclickListener();
		MyOnclickListener2 listen2 = new MyOnclickListener2();
		btn.setOnClickListener(l);
		btn2.setOnClickListener(l);
		btn4.setOnClickListener(l);
		btn3.setOnClickListener(listen2);// 取消注册广播
	}

	class MyOnclickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (R.id.btn02 == v.getId()) {
				BCReceiver r = new BCReceiver();
				IntentFilter filter = new IntentFilter();
				filter.addAction(ACTION_SMS_RECEIVE);
				System.out.println("注册广播拦截,btn02");
				MainActivity.this.registerReceiver(r, filter);
			} else if (R.id.btn01 == v.getId()) {
				Intent intent = new Intent();
				intent.setAction(ACTION_SMS_RECEIVE);
				System.out.println("自己发送广播");
				MainActivity.this.sendBroadcast(intent);

			} else if (R.id.btn03 == v.getId()) {
				BCReceiver r = new BCReceiver();
				MainActivity.this.unregisterReceiver(r);
			} else if (R.id.btn04 == v.getId()) {
				// 拦截电话呼出
				// android.intent.action.NEW_OUTGOING_CALL
				BCReceiver2 r04 = new BCReceiver2();
				IntentFilter filter = new IntentFilter();
				filter.addAction(ACTION_CALL_OUT);
				MainActivity.this.registerReceiver(r04, filter);
			}
		}

	}

	class MyOnclickListener2 implements OnClickListener {
		@Override
		public void onClick(View v) {
			System.out.println("listen2,取消注册广播");
			MainActivity.this.unregisterReceiver(re);
		}
	}
}

  短信对应的receiver,

package com.wyl.bctest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;

public class BCReceiver extends BroadcastReceiver {
	public BCReceiver() {
		// TODO Auto-generated constructor stub
		System.out.println("BCReceiver短信构造器。。。");
	}

	@Override
	public void onReceive(Context arg0, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onReceiver短信复写方法。。。。");
		Bundle bundle = intent.getExtras();
		Object messages[] = (Object[]) bundle.get("pdus");
		SmsMessage mSmsMessage[] = new SmsMessage[messages.length];
		for (int i = 0; i < messages.length; i++) {
			mSmsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
		}
		System.out.println("短信的具体内容"+mSmsMessage.toString());
	}

}

  电话拦截相关的receiver,

package com.wyl.bctest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BCReceiver2 extends BroadcastReceiver{
	public BCReceiver2() {
		// TODO Auto-generated constructor stub
		System.out.println("BCReceiver电话拦截构造器。。。");
	}
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub
		System.out.println("onReceiver电话拦截复写方法。。。。");

	}

}

  注意点就是:

1.在AndroidMainfest.xml中注册短信接收和电话呼出的权限。

2.在Java代码中注册拦截的时候IntentFilter filter,其中filter.addAction(param)的参数param相当于AndroidMainfest.xml中的

<receiver android:name="com.wyl.jqr.TestReceiver">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

  

android.provider.Telephony.SMS_RECEIVED这个String字符串,AndroidMainifest.xml的整个配置如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wyl.bctest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
	<uses-permission android:name="android.permission.RECEIVE_SMS"/>
	<!-- 增加拦截电话的权限 -->
	<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.wyl.bctest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
        	<receiver
            android:name="com.wyl.bctest.BCReceiver"
            ></receiver>
         -->
    </application>

</manifest>

  

时间: 2024-10-11 05:47:55

Android短信拦截和电话拦截的相关文章

向Android虚拟机发送短信和拨打电话

Android的emulator是已经包含了gsm 模块,可以模拟电话与短信进行调试(就不用花太多冤枉钱) 首先,肯定是打开虚拟机: emulator -avd XXXXXX -scale 0.8& 这两个参数就不用说了,第一个是avd 名称,第二个是显示大小. 然后使用: telnet localhost <port> port 是虚拟机端口号,一般为5554 (可以使用adb devices 查看) 完整的写法是:telnet localhost 5554. 这时,我们就进入了An

Android短信彩信收发流程(应用层)

下图为ComposeMessageActivity中confirmSendMessageIfNeeded部分的信息发送流程.主要以接收者有效性的确认为主,然后转向sendMessage方法进行发送. ComposeMessageActivity.sendMessage从下图可以看出,在这个方法中,主要做的事是确认手机状态的有效性.最终调用WorkingMessage的send方法进行信息的发送.  WorkingMessage.send从下图可以看出,在本方法中,对于不同类型的消息,分别调用不同

Android短信监听(三)——利用Loader实现短信监听

MainActivity如下: package cc.c; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; import android.content.Loader; import android.database.Curs

Android短信监听(二)——利用ContentObserver实现短信监听

MainActivity如下: package cc.testsmslistener; import cc.testsmslistener.SMSContentObserver.MessageListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import android.app.Activity; /** *

Android短信监听(一)——利用BroadcastReceiver实现短信监听

MainActivity如下: package cc.testsmsbroadcastreceiver; import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; /** * Demo描述: * 利用BroadcastReceiver实现

Android -- 短信

背景                                                                                           主要代码                                                                                      发送按钮的监听器: phone_number_editText = (EditText) findViewById(R.id.phon

初识安卓小程序(Android短信发送器)

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"短信发送器" 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tool

Android短信插入器源码

package com.examp.createsms; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.net.Uri; import android.os.Bundle; public class MainActivity extends Activity { @Override protected

Android 短信的还原

上篇文章讲到<Android 短信的备份>,本文主要实现Android 短信的还原,即是将一条 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andr