Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207905
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-11 07:20:01

01_22_广播机制(二)





点击绑定,然后给号码123发送信息hello


点击(此处)折叠或打开

  1. //SMSReceiver.java
  2. package mars.testbc2;

  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.telephony.SmsMessage;

  8. public class SMSReceiver extends BroadcastReceiver{

  9.     /*
  10.      * 1、创建一个Bundle:bundle = intent.getExtras();
  11.      *     Bundle用于Activity间通信,bundle类中加入数据(key -value的形式,
  12.      * 另一个activity里面取数据的时候,就要用到key,找出对应的value)
  13.      * 详细见:http://blog.csdn.net/randyjiawenjie/article/details/6651437
  14.      * 2、创建一个Object[]数组从Bundle中取数据:myoBJpdus = (OBject[])bundle.get("pdus");
  15.      *     Object[]数组用法详见:http://blog.csdn.net/linbin130209114/article/details/6150143
  16.      * 3、创建一个大小为myOBJpdus.length的SmsMessage[]数组用于存放短信:
  17.      *     messages = new SmsMessage[myOBJpdus.length];
  18.      * 4、从Object[]数组中得到短信:
  19.      *     message = new SmsMessage.createFromPdu((byte[])myOBJpdus[i]);
  20.      * //使用pdu格式的短信数据生成短信对象
  21.      * 5、显示短信System.out.println(messages[i].getDisplayMessageBody());
  22.      * */
  23.     @Override
  24.     public void onReceive(Context context, Intent intent) {
  25.         // TODO Auto-generated method stub
  26.         System.out.println("receive message");

  27.         //接受Intent对象当中的数据
  28.         Bundle bundle = intent.getExtras();
  29.         //在Bundle对象当中有一个属性名为pdus,这个属性的值是一个Object数组
  30.         Object[] myOBJpdus = (Object[]) bundle.get("pdus");
  31.         //创建一个SmsMessage类型的数组
  32.         SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
  33.         System.out.println(messages.length);
  34.         for (int i = 0; i<myOBJpdus.length; i++)
  35.         {
  36.           //使用Object数组当中的对象创建SmsMessage对象
  37.           messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
  38.           //调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
  39.           System.out.println(messages[i].getDisplayMessageBody());
  40.         }
  41.         try {
  42.         //    Thread.sleep(30 * 1000);
  43.             Thread.sleep(3);
  44.             System.out.println("-------------------------------");
  45.         } catch (InterruptedException e) {
  46.             // TODO Auto-generated catch block
  47.             e.printStackTrace();
  48.         }
  49.     }

  50. }

点击(此处)折叠或打开

  1. //TestBC2Activity.java
  2. package mars.testbc2;

  3. import android.app.Activity;
  4. import android.content.IntentFilter;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;

  9. public class TestBC2Activity extends Activity {
  10.     /** Called when the activity is first created. */
  11.     private Button registerButton = null;
  12.     private Button unregisterButton = null;
  13.     private SMSReceiver smsReceiver = null;
  14.     
  15.     private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.main);
  20.         registerButton = (Button)findViewById(R.id.register);
  21.         registerButton.setOnClickListener(new RegisterReceiverListener());
  22.         unregisterButton = (Button)findViewById(R.id.unregister);
  23.         unregisterButton.setOnClickListener(new UnRegisterReceiverListener());
  24.     }
  25.     /*绑定监听器
  26.      * 1、创建一个BacastReciver对象:smsReceiver = new SMSReceiver()
  27.      * 2、创建一个IntentFilter对象:intentFilter = new IntentFilter()
  28.      * 3、为过滤器intentFilter添加Action:filter.addAction(SMS_ACTiON)
  29.      * 4、注册广播:TestBC2Activity.this.registerReceiver(smsReceiver,filter)
  30.      * 这样一个广播smsReceiver和filter就一一对应啦*/
  31.     class RegisterReceiverListener implements OnClickListener{
  32.         @Override
  33.         public void onClick(View v) {
  34.             System.out.println("click********RegisterReceiverListener");
  35.             //生成一个BroiadcastReceiver对象
  36.             smsReceiver = new SMSReceiver();
  37.             //生成一个IntentFilter对象
  38.             IntentFilter filter = new IntentFilter();
  39.             //为IntentFilter添加一个Action
  40.             filter.addAction(SMS_ACTION);
  41.             //将BroadcastReceiver对象注册到系统当中
  42.             TestBC2Activity.this.registerReceiver(smsReceiver, filter);
  43.         }     
  44.     }
  45.     
  46.     class UnRegisterReceiverListener implements OnClickListener{
  47.         @Override
  48.         public void onClick(View v) {
  49.             System.out.println("click********unRegisterReceiverListener");
  50.             //解除BroadcastReceiver对象的注册
  51.             TestBC2Activity.this.unregisterReceiver(smsReceiver);
  52.         }     
  53.     }
  54. }

也可以在AndroidMainfext.xml中注册receiver

点击(此处)折叠或打开

  1. <!--<receiver android:name="SMSReceiver">
  2.             <intent-filter>
  3.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  4.             </intent-filter>
  5.         </receiver>-->

结果:
04-10 23:12:44.312: I/System.out(330): click********RegisterReceiverListener
04-10 23:12:56.910: I/System.out(330): click********RegisterReceiverListener
04-10 23:13:29.990: I/System.out(330): receive message
04-10 23:13:30.000: I/System.out(330): 1
04-10 23:13:30.050: I/System.out(330): hello
04-10 23:13:30.120: I/System.out(330): -------------------------------
04-10 23:13:30.130: I/System.out(330): receive message
04-10 23:13:30.130: I/System.out(330): 1
04-10 23:13:30.141: I/System.out(330): hello
04-10 23:13:30.215: I/System.out(330): -------------------------------
04-10 23:14:02.320: I/System.out(330): click********unRegisterReceiverListener


阅读(682) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~