android > BroadcastReceiver (广播) >2

通过 ,应用程序代码中注册  特点是, 可以 针对一些 需要在 应用程序打开时 ,更新 的 , 比如 UI变化在 ,ACTIVITY 不可见的时候取消注册

 

关于 来短信 的 广播 broadcast

broad3.com

package broad3.com;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Filter;

public class Broad3Activity extends Activity {
    /** Called when the activity is first created. */
	private Button reg_button;
	private Button unreg_button;	
	private SMSReceiver smsReceiver;
	//这个声明的一个静态字符串 类似 manifest 的 receiver 的 filter类型
	private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED"; 	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        reg_button = (Button)findViewById(R.id.reg_button);
        reg_button.setOnClickListener(new RegBroadcast());
        unreg_button = (Button)findViewById(R.id.unreg_button);
        unreg_button.setOnClickListener(new UnregBraodcast());
    }
    
    class RegBroadcast implements OnClickListener{
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		smsReceiver = new SMSReceiver(); //里面是 继承BroadcastReceiver的类
    		IntentFilter filter = new IntentFilter(); //action 类型的过滤
    		filter.addAction(SMS_ACTION);	//把过滤类型 添加进去
    		Broad3Activity.this.registerReceiver(smsReceiver, filter);	//注册braodCast
    		System.out.println("绑定成功");
    	}
    }
    
    class UnregBraodcast implements OnClickListener{
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		//解除绑定
    		Broad3Activity.this.unregisterReceiver(smsReceiver);
    	}
    	
    }   
    
    
}

 

需要在 manifest 里面 给 接受短信 广播的 全选

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

 SMSReceiver.java 里面是继承了 braodcastreceiver 的类 

package broad3.com;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver{
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receive message");
		
		//接收intent 对象中的数据
		Bundle bundle = intent.getExtras();
		
		//在bandle 对象当中有一个属性名为 pdus 的属性,这个属性的值是一个 object 数组		
		Object[] myObjects = (Object[]) bundle.get("pdus");
        SmsMessage[] messages = new SmsMessage[myObjects.length];  
        System.out.println(messages.length);
        for (int i = 0; i<myObjects.length; i++) 
        {  
          //使用Object数组当中的对象创建SmsMessage对象
          messages[i] = SmsMessage.createFromPdu((byte[]) myObjects[i]);  
          //调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
          System.out.println(messages[i].getDisplayMessageBody());
        }
        try {
			Thread.sleep(30 * 1000);
			System.out.println("-------------------------------");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

}
 

 

你可能感兴趣的:(android > BroadcastReceiver (广播) >2)