你的位置:首页 > 软件开发 > 操作系统 > Android入门:广播发送者与广播接收者

Android入门:广播发送者与广播接收者

发布时间:2017-09-07 21:00:11
参考:Android入门:广播发送者与广播接收者 - xiazdong - CSDN博客BroadcastReceiver类,重写:public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;广播意 ...

参考:

Android入门:广播发送者与广播接收者 - xiazdong - CSDN博客
BroadcastReceiver类,重写:

public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;

广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action="com.xiazdong",则所有在AndroidManifest.

 

:onReceive方法必须在10秒内完成,如果没有完成,则抛“Application No Response”当广播接收者onReceive方法需要执行很长时间时,最好将此耗时工作通过Intent发送给Service由Service完成,并且不能使用子线程解决,因为BroadcastReceiver是接收到广播后才创建的,并且生命周期很短,因此子线程可能在没有执行完就已经被杀死了。

 

 

[java] view plain copy 
  1. public void onReceive(Context context,Intent intent){  
  2.     Intent intent = new Intent(context,XxxService.class);  
  3.     context.startService(intent);  
  4. }  



 

2.广播发送者

 

通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序;

通常广播发送方都是通过隐式意图,这样才能发送给多人;

 

广播发送方分为普通广播和有序广播;

同步广播:发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播;Context.sendBroadcast(intent);

有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.

 

 

二、广播接收者核心代码

 

同步广播发送方核心代码:

 

 

[java] view plain copy 
  1. Intent intent = new Intent();  
  2. intent.setAction("...");  
  3. Context.sendBroadcast(intent);  



 

有序广播发送方核心代码:

 

 

[java] view plain copy 
  1. Intent intent = new Intent();  
  2. intent.setAction("...");  
  3. Context.sendOrderedBroadcast(intent,null);  



 

广播接收者核心代码:

 

 

[java] view plain copy 
  1. public class Receiver extends BroadcastReceiver{  
  2.     public void onReceive(Context context, Intent intent) {  
  3.         Bundle bundle = intent.getExtras();  
  4.         ...  
  5.     }  
  6. }  

 

AndroidManifest.

 

[html] view plain copy 
  1. <application>           
  2.     <receiver android:name=".Receiver">   
  3.         <intent-filter android:priority="1000">   
  4.             <action android:name="com.xiazdong"/>  
  5.         </intent-filter>  
  6.     </receiver>  
  7. </application>          



 

 

三、广播实例

 

 

1.同步广播实例

 

 

场景说明:

 

Android入门:广播发送者与广播接收者

 

(1)广播发送者:

 

 

[java] view plain copy 
  1. package com.xiazdong.broadcastsender;  
  2.   
  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. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private Button button;  
  13.     private OnClickListener listener = new OnClickListener(){  
  14.         @Override  
  15.         public void onClick(View v) {  
  16.             Intent intent = new Intent();  
  17.             intent.setAction("com.xiazdong");  
  18.             intent.putExtra("name", "xiazdong");  
  19.             MainActivity.this.sendBroadcast(intent);  
  20.             Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();  
  21.         }  
  22.     };  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         button = (Button)this.findViewById(R.id.button);  
  28.         button.setOnClickListener(listener);  
  29.     }  
  30. }  



 

(2)广播接收者

 

 

[java] view plain copy 
  1. package com.xiazdong.broadcastreceiver1;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. public class Receiver extends BroadcastReceiver {  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         String name = intent.getExtras().getString("name");  
  13.         Log.i("Recevier1", "接收到:"+name);  
  14.     }  
  15.   
  16. }  



 

AndroidManifest.

 

 

[java] view plain copy 
  1. <receiver android:name=".Receiver">  
  2.         <intent-filter>  
  3.              <action android:name="com.xiazdong"/>  
  4.         </intent-filter>  
  5. </receiver>  



 

结果:

 

Android入门:广播发送者与广播接收者

 

 

2.有序广播实例

 

 

场景说明:

Android入门:广播发送者与广播接收者

 

(1)广播发送者

 

 

[java] view plain copy 
  1. package com.xiazdong.broadcastsender;  
  2.   
  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. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private Button button;  
  13.     private OnClickListener listener = new OnClickListener(){  
  14.         @Override  
  15.         public void onClick(View v) {  
  16.             Intent intent = new Intent();  
  17.             intent.setAction("com.xiazdong");  
  18.             intent.putExtra("name", "xiazdong");  
  19.             MainActivity.this.sendOrderedBroadcast(intent, null);   //有序广播发送  
  20.             Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();  
  21.         }  
  22.     };  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         button = (Button)this.findViewById(R.id.button);  
  28.         button.setOnClickListener(listener);  
  29.     }  
  30. }  



 

(2)广播接收者


Receiver1

 

[java] view plain copy 
  1. package com.xiazdong.broadcastreceiver1;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. public class Receiver extends BroadcastReceiver {  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         String name = intent.getExtras().getString("name");  
  13.         Log.i("Recevier1", "接收到:"+name);  
  14.         abortBroadcast();   //Receiver1接收到广播后中断广播  
  15.     }  
  16.   
  17. }  



 

AndroidManifest.

 

 

[html] view plain copy 
    1. <receiver android:name=".Receiver">  
    2.       <intent-filter android:priority="1000"> <!-- 设置最高优先级 -->  
    3.          <action android:name="com.xiazdong"/>  
    4.       </intent-filter>  
    5.  </receiver>  

 

原标题:Android入门:广播发送者与广播接收者

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录