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

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-10 20:47:07

01_21_广播机制(一)






点击(此处)折叠或打开

  1. //TestActivity.java
  2. package mars.testbc;

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

  9. public class TestActivity extends Activity {
  10.     /** Called when the activity is first created. */
  11.     private Button sendButton;
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.testreceiver);
  16.         sendButton = (Button)findViewById(R.id.sendButton);
  17.         sendButton.setOnClickListener(new BroadcastListener());
  18.     }
  19.     /*发送广播
  20.      * 1、创建一个Intent对象:intent = new Intent
  21.      * 2、设置广播动作intent.setAction(ITent.ACTION_EDIT)
  22.      * 3、发送广播:TestActivity.this.sendBroadcast(intent)*/
  23.     class BroadcastListener implements OnClickListener{

  24.         @Override
  25.         public void onClick(View v) {
  26.             System.out.println("Click --- button ");
  27.             Intent intent = new Intent();
  28.             intent.setAction(Intent.ACTION_EDIT);
  29.             TestActivity.this.sendBroadcast(intent);
  30.         }
  31.         /*注意:上面的intent.setAction(Intent.ACTION_EDIT);和
  32.          * AndroidMainfext.xml中的是对应的
  33.         
  34.             
  35.                 
  36.             
  37.         */
  38.     }
  39. }

点击(此处)折叠或打开

  1. //TestReceiver.java
  2. package mars.testbc;

  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;

  6. /*TestReceiver继承了BroadcastReceiver并复写了onReceive()方法
  7.  *
  8.  * 处理广播消息*/
  9. public class TestReceiver extends BroadcastReceiver{

  10.     public TestReceiver(){
  11.         System.out.println("TestReceiver");
  12.     }
  13.     @Override
  14.     public void onReceive(Context context, Intent intent) {
  15.         System.out.println("onReceive");
  16.     }
  17. }


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="mars.testbc" android:versionCode="1" android:versionName="1.0">
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  5.         <activity android:name=".TestActivity" android:label="@string/app_name">
  6.             <intent-filter>
  7.                 <action android:name="android.intent.action.MAIN" />
  8.                 <category android:name="android.intent.category.LAUNCHER" />
  9.             </intent-filter>
  10.         </activity>
  11.         <receiver android:name=".TestReceiver">
  12.             <intent-filter>
  13.                 <action android:name="android.intent.action.EDIT" />
  14.             </intent-filter>
  15.         </receiver>
  16.     </application>
  17.     <uses-sdk android:minSdkVersion="4" />
  18.     <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  19. </manifest>

结果如下:(从结果可知每次广播信息均重新生成TestReceiver并调用onReceive方法
04-10 12:31:55.597: I/System.out(511): Click --- button 
04-10 12:31:55.706: I/System.out(511): TestReceiver
04-10 12:31:55.706: I/System.out(511): onReceive
04-10 12:31:59.567: I/System.out(511): Click --- button 
04-10 12:31:59.607: I/System.out(511): TestReceiver
04-10 12:31:59.607: I/System.out(511): onReceive

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