androi-BroadcastReceiver<九>

1.BroadcastReceiver(抽象类)

  它android的四大组件之一,所以要在清单文件中注册<receiver>,用来发送消息,只负责发送,不管是否接受到或如何处理。

  构造方法:可以自己写一个继承该BroadcastReceiver的类去写入自己想要的内容。

  成员方法(只会执行该方法):

         void onReceive(Context context, Intent intent) ;//context是指发起该intent的组件(如Activity)

2.BroadcastReceiver生命周期

   每当调用BroadcastReceiver时,它就会创建一个该类的对象(执行构造方法),并调用onReceive方法

   之后该对象就被消除。只有再次调用BroadcastReceiver时,才会重新创建一个该类的对象。

 1  1 public class TestReceiverActivity extends Activity {
 2  2     /** Called when the activity is first created. */
 3  3     private Button bt=null;
 4  4     @Override
 5  5     public void onCreate(Bundle savedInstanceState) {
 6  6         super.onCreate(savedInstanceState);
 7  7         setContentView(R.layout.main);
 8  8         bt=(Button)findViewById(R.id.bt);
 9  9         bt.setOnClickListener(new  View.OnClickListener(){
10 10 
11 11             @Override
12 12             public void onClick(View v) {
13 13                 // TODO Auto-generated method stub
14 14                 //生成一个intent,并设置它的动作,只有与在清单文件中
15 15                 //intent-filter里匹配的动作,才会执行包裹该intent-filter的组件(broadcast)
16 16                 Intent  it=new Intent();
17 17                 it.setAction(Intent.ACTION_EDIT);//该参数(字符串)可以任意内容,只要和intent-filter里的匹配即可
18 18                 //发送一个与该intent匹配的broadcast
19 19                 sendBroadcast(it);
20 20             }
21 21             
22 22         });
23 23     }
24 24 }
View Code

   继承BroadcastReceiver类

 1 package hq.TestReceiver;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 
 7 public class TestBroadcast extends BroadcastReceiver{
 8     
 9     public TestBroadcast()
10     {
11         System.out.println("TestBroadcast构造");
12     }
13     @Override
14     public void onReceive(Context context, Intent intent) {
15         // TODO Auto-generated method stub
16         System.out.println("TestBroadcast-onReceive");
17     }
18 
19 }
View Code

  清单文件xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="hq.TestReceiver"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="15" />
 8 
 9     <application
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name" >
12         <activity
13             android:name=".TestReceiverActivity"
14             android:label="@string/app_name" >
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <receiver 
22             android:name=".TestBroadcast"
23             >
24             <intent-filter >
25                <action android:name="android.intent.action.EDIT"/>
26               <category android:name = "android.intent.category.DEFAULT"  />  
27             </intent-filter>
28             
29         </receiver>
30     </application>
31 
32 </manifest>
View Code

3.注册BroadcastReceiver的方法

  a.在xml注册:如上<receiver/><intent-filter/>,通过intent来找到匹配它的接受器。

  b.在应用程序中的代码进行注册:下面两个方法来自Activity(Context组件)

       进行注册: registerReceiver(receivedr,filter);//这里filter的action必须是android内部指定已有的。

          好处:可以保证在应用程序安装之后,BroadcastReceiver始终处于活动状态,通常用于监听系统状态的改变,

             比如说手机的电量,对于这样的BroadcastReceiver,通常是在产生某个特定的系统事件之后,进行相应

             的操作,比如说wifi网卡打开时,给用户一个提示

       取消注册:registerReceiver(receivedr);  //因为你没有xml注册相应的action,则需要系统内置的action帮你

          好处:如果一个BroadcastReceiver用于更新UI,那么最好以这种方式注册,

                  在Activity启动时注册它,在Activity不可见以后取消注册它。

4.显示intent和隐式intent

   显示intent:指定调用某个Activity(组件),并直接调用该Activity。

 1  //响应按钮btn事件  
 2         btn.setOnClickListener(new OnClickListener() {        
 3             @Override 
 4             public void onClick(View v) {  
 5                 //显示方式声明Intent,直接启动SecondActivity  
 6                 Intent it = new Intent(MainActivity.this,SecondActivity.class);  
 7                 //启动Activity  
 8                 startActivity(it);            
 9             }  
10         });  

    隐式intent:调用匹配该intent的动作所有Activity(组件),这些匹配对应的Activity都写在清单文件中。

                     注意:隐式intent的intent-filter里必须有这个规定的东西:意思是该intent是隐式的方式

                     <category android:name = "android.intent.category.DEFAULT"  /> 

     1、一个 Intent 可以有多个 category,但至少会有一个,也是默认的一个 category。
     2、只有 Intent 的所有 category 都匹配上,Activity 才会接收这个 Intent。

    参考博客:http://liangruijun.blog.51cto.com/3061169/655132

 

posted @ 2015-02-16 10:42  beyondbycyx  阅读(135)  评论(0编辑  收藏  举报