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

      

  • 相关阅读:
    【STM32F429】第1章 当前主流的小型嵌入式GUI
    【STM32H7】第14章 UDP用户数据报协议基础知识
    【STM32F429】第14章 UDP用户数据报协议基础知识
    【STM32F407】第14章 UDP用户数据报协议基础知识
    【STM32H7】第13章 RL-TCPnet V7.X之创建多个TCP客户端
    【STM32F429】第13章 RL-TCPnet V7.X之创建多个TCP客户端
    【STM32F407】第13章 RL-TCPnet V7.X之创建多个TCP客户端
    【STM32H7】第12章 RL-TCPnet V7.X之TCP客户端
    【STM32F429】第12章 RL-TCPnet V7.X之TCP客户端
    迄今最强综合Demo,H7-TOOL的RTX5全家桶版本整体设计框架
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4687902.html
Copyright © 2020-2023  润新知