博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android广播——短信拦截
阅读量:5333 次
发布时间:2019-06-15

本文共 2957 字,大约阅读时间需要 9 分钟。

  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.....");	}	}

  

转载于:https://www.cnblogs.com/Sunnor/p/4687902.html

你可能感兴趣的文章
H5多文本换行
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
Swift和OC混编
查看>>
Android轻量级的开源缓存框架ASimpleCache
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>
mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享
查看>>
分层图最短路【bzoj2763】: [JLOI2011]飞行路线
查看>>
linux下编译复数类型引发的错误:expected unqualified-id before '(' token
查看>>
codeforces 1041A Heist
查看>>