android中广播的两种启动方式浅析【自定义Action】

1:注册广播:(有两种方式)

     一是动态方式使用Context.registerReceiver()方法来注册;
     二是静态方式,即在AndroidManifest.xml中通过<receiver> 。

2:发送广播:

      在需要广播的地方sendBroadcast(intent); 之前要给Intent的对象intent 设置Action (Action可以被系统的或自定义的)和 data。

3:接收广播:

           就是在extends Broadcast的那个类中, onReceive(Context context, Intent intent)方法要做的事情。


***静态方式*****

main.xml

?
<? xml  version = "1.0"  encoding = "utf-8" ?> 
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android" 
     android:layout_width = "fill_parent" 
     android:layout_height = "fill_parent" 
     android:orientation = "vertical" 
   
     < Button 
         android:id = "@+id/b" 
         android:layout_width = "fill_parent" 
         android:layout_height = "wrap_content" 
         android:text = "发送"  /> 
     < Button 
         android:id = "@+id/cancle" 
         android:layout_width = "fill_parent" 
         android:layout_height = "wrap_content" 
         android:text = "exit"  /> 
   
</ LinearLayout
Activity:

public  class  TestActivity extends  Activity {
     
     private  Button b;
     private  Button cancle;
     public  static  final  String MY_ACTION = "mxp" ;
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         b = (Button) findViewById(R.id.b);
         cancle = (Button) findViewById(R.id.cancle);
         
         b.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 // TODO Auto-generated method stub
                 Intent intent  = new  Intent();
                 intent.setAction(MY_ACTION);
                 intent.putExtra( "msg" , "asdsdas" );
                 sendBroadcast(intent);
             }
         });
         cancle.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 finish();
             }
         });
     }
}

  AndroidManifest.xml

?
<?xml version= "1.0"  encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
     package = "m.m.m"
     android:versionCode= "1"
     android:versionName= "1.0"  >
 
     <uses-sdk android:minSdkVersion= "8"  />
 
     <application
         android:icon= "@drawable/ic_launcher"
         android:label= "@string/app_name"  >
         <activity
             android:name= ".TestActivity"
             android:label= "@string/app_name"  >
             <intent-filter>
                 <action android:name= "android.intent.action.MAIN"  />
 
                 <category android:name= "android.intent.category.LAUNCHER"  />
             </intent-filter>
         </activity>
         
         <receiver android:name= ".BroadCastActivity" >
             <intent-filter >
                 <action android:name= "mxp" />
             </intent-filter>
         </receiver>
         
     </application>
 
</manifest>

 总结 :自定义Action,就是定义一个静态常量如:public static final String MY_ACTION = "mxp";但是不要和系统的重复哦!

                对我而言,关键是注册,即

                 <receiver android:name=".BroadCastActivity">  注册广播

                       <intent-filter >

                            <action android:name="mxp"/> 定义action

                       </intent-filter>

               </receiver>

 

***动态方式:*************

 1)不要androidManifest.xml文件中注册 ,即删除刚才注册的部分

  2) 在启动服务的地方,即发送按钮的监听器里注册,再启动服务

  TestActivity.java 多了注册的代码:myReceiver = new MyReceiver();
                             IntentFilter filter = new IntentFilter();
                           filter.addAction(MY_ACTION);


import  android.app.Activity;
import  android.content.Intent;
import  android.content.IntentFilter;
import  android.os.Bundle;
import  android.view.View;
import  android.view.View.OnClickListener;
import  android.widget.Button;
 
public  class  TestActivity extends  Activity {
     
     private  Button bind;
     private  Button unbind;
     private  Button cancle;
     protected  MyReceiver myReceiver;
     public  static  final  String MY_ACTION = "mxp" ;
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         bind = (Button) findViewById(R.id.bind);
         unbind = (Button) findViewById(R.id.unbind);
         cancle = (Button) findViewById(R.id.cancle);
         
         bind.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 // TODO Auto-generated method stub
                 
                 myReceiver = new  BroadCastActivity();
                 IntentFilter filter = new  IntentFilter();
                 filter.addAction(MY_ACTION);
                 
                 TestActivity. this .registerReceiver(myReceiver, filter);
                 System.out.println( "bind" );
                 
                 Intent intent  = new  Intent();
                 intent.setAction(MY_ACTION);
                 intent.putExtra( "msg" , "asdsdas" );
                 sendBroadcast(intent);
             }
         });
         unbind.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 TestActivity. this .unregisterReceiver(myReceiver);
                 System.out.println( "unbind" );
             }
         });
         cancle.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 finish();
             }
         });
     }
     
     @Override
     protected  void  onResume() {
         // TODO Auto-generated method stub
         super .onResume();
     }
}

  main.xml

?
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:orientation= "vertical"  >
 
     <Button
         android:id= "@+id/bind"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "绑定"  />
     <Button
         android:id= "@+id/unbind"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "取消绑定"  />
     <Button
         android:id= "@+id/cancle"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "exit"  />
 
</LinearLayout>

 总结:我在测试时点击绑定怎么都不显示Toast的内容,原因是我只注册了广播,没有发送广播。要想广播响应,一定要注册还要发送






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值