9.BroadcastReceiver的使用

普通BroadcastReceiver

核心代码:

组件:

public class SMSReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receive message");
		
	}
}


组件调用:

			//生成一个BroiadcastReceiver对象
			SMSReceiver smsReceiver  = new SMSReceiver();
			//生成一个IntentFilter对象
			IntentFilter filter = new IntentFilter();
			//为IntentFilter添加一个Action
			filter.addAction("android.provider.Telephony.SMS_RECEIVED");
			//设置哪个(SMSReceiver)组件接收android.provider.Telephony.SMS_RECEIVED类型的广播
			TestBC2Activity.this.registerReceiver(smsReceiver, filter);
			//解除BroadcastReceiver对象的注册
			TestBC2Activity.this.unregisterReceiver(smsReceiver);


控件图例:


具体应用:

组件:

package mars.testbc2;

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();
		//在Bundle对象当中有一个属性名为pdus,这个属性的值是一个Object数组
		//pdu为一条短信,pdus为所有短信
		Object[] myOBJpdus = (Object[]) bundle.get("pdus"); 
		//创建一个SmsMessage类型的数组
        SmsMessage[] messages = new SmsMessage[myOBJpdus.length];  
        System.out.println(messages.length);
        for (int i = 0; i<myOBJpdus.length; i++) 
        {  
          //使用Object数组当中的对象创建SmsMessage对象
          messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);  
          //调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
          System.out.println(messages[i].getDisplayMessageBody());
        }
	}

}


组件调用:

package mars.testbc2;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestBC2Activity extends Activity {
    /** Called when the activity is first created. */
	private Button registerButton = null;
	private Button unregisterButton = null;
	private SMSReceiver smsReceiver = null;
	
	private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerButton = (Button)findViewById(R.id.register);
        registerButton.setOnClickListener(new RegisterReceiverListener());
        unregisterButton = (Button)findViewById(R.id.unregister);
        unregisterButton.setOnClickListener(new UnRegisterReceiverListener());
    }
    
    class RegisterReceiverListener implements OnClickListener{

		public void onClick(View v) {
			//生成一个BroiadcastReceiver对象
			smsReceiver  = new SMSReceiver();
			//生成一个IntentFilter对象
			IntentFilter filter = new IntentFilter();
			//为IntentFilter添加一个Action
			filter.addAction(SMS_ACTION);
			//将BroadcastReceiver对象注册到系统当中
			TestBC2Activity.this.registerReceiver(smsReceiver, filter);
			
		}
    	
    }
    
    class UnRegisterReceiverListener implements OnClickListener{

		public void onClick(View v) {
			//解除BroadcastReceiver对象的注册
			TestBC2Activity.this.unregisterReceiver(smsReceiver);
		}
    	
    }
}


项目文件地址:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值