Android广播——短信拦截

  MainActivity.java

package com.example.broadcasttest;

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

public class MainActivity extends ActionBarActivity {
	Button btn01;
	Button btn02;
	Button btn03;
	Button btn04;
	private final static String ACTION_SMS = "android.provider.Telephony.SMS_RECEIVED";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn01 = (Button)findViewById(R.id.btn01);
		btn02 = (Button)findViewById(R.id.btn02);
		btn03 = (Button)findViewById(R.id.btn03);
		btn04 = (Button)findViewById(R.id.btn04);
		btn01.setOnClickListener(new myListenr());
		btn02.setOnClickListener(new myListener2());
		btn03.setOnClickListener(new myListenr());
		btn04.setOnClickListener(new myListenr());
	}
	
	class myListenr implements OnClickListener{

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			if(R.id.btn01==view.getId()){
				//开启广播
				Intent intent = new Intent();
//				intent.setAction(Intent.ACTION_EDIT);//
				intent.setAction(Intent.ACTION_SEARCH);
				MainActivity.this.sendBroadcast(intent);
				
			}else if(R.id.btn03==view.getId()){
				//开启广播
				Intent intent = new Intent();
				intent.setAction(ACTION_SMS);
				System.out.println("准备模拟发送短信");
				MainActivity.this.sendBroadcast(intent);
				
			}else if(R.id.btn04==view.getId()){
				//开启广播
				TestBCReceiver02 receiver = new TestBCReceiver02();
				System.out.println("==取消监听==");
				MainActivity.this.unregisterReceiver(receiver);//这里会导致app崩溃,暂时不知道原因
			}
		}
		
		
	}
	class myListener2 implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			//注册广播事件
			//1.获取recevier
			TestBCReceiver02 receiver = new TestBCReceiver02();
			//2.生成一个IntentFilter对象
			IntentFilter filter = new IntentFilter();
			//3.为intentFilter添加一个action 
			filter.addAction(ACTION_SMS);
			MainActivity.this.registerReceiver(receiver, filter);
		}
		
	}
}

  Recvier的实现类:

package com.example.broadcasttest;

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

public class TestBCReceiver02 extends BroadcastReceiver{
	public TestBCReceiver02(){
		System.out.println("BC02的构造器开始了,。。。。。");
	}
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		System.out.println("收到短信了");
	}
	
}

  Receiver实现类2

package com.example.broadcasttest;

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

public class TestBroadcastReceiver extends BroadcastReceiver{
	
	public TestBroadcastReceiver(){
		System.out.println("receiver开始了,,,,");
	}
	
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		System.out.println("onReceive.....");
	}
	
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值