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

      

  • 相关阅读:
    Java基础之:构造方法(构造器)与this关键字
    Java基础之:属性与局部变量作用域
    Java基础之:方法重载与可变参数
    Java基础之:成员方法与传参机制
    Java基础之:类与对象
    Java基础之:数组练习题
    Java基础之:二维数组
    Promise对象的简单用法
    如何用vue-cli初始化一个vue项目
    get和post请求的区别
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4687902.html
Copyright © 2020-2023  润新知