码迷,mamicode.com
首页 > 移动开发 > 详细

Android短信拦截和电话拦截

时间:2015-07-30 12:51:13      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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>

  

Android短信拦截和电话拦截

标签:

原文地址:http://www.cnblogs.com/Sunnor/p/4688698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!