Android_Broadcast
Posted Tears_fg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android_Broadcast相关的知识,希望对你有一定的参考价值。
Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.BROADCAST_STICKY"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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="com.example.android_broadcast.MyBroadcastReceiver1"> <intent-filter android:priority="200"> <action android:name="broadcast_one"/> </intent-filter> </receiver> <receiver android:name="com.example.android_broadcast.MyBroadcastReceiver2"> <intent-filter android:priority="100"> <action android:name="broadcast_one"/> </intent-filter> </receiver> </application> </manifest>
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_broadcast.MainActivity" > <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="doClick" android:text="发送一条普通广播"/> <Button android:id="@+id/btn_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="doClick" android:text="发送一条有序广播"/> <Button android:id="@+id/btn_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="doClick" android:text="发送一条异步广播"/> </LinearLayout>
源代码MainActivity:
package com.example.android_broadcast; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; /** * Broadcast(广播):是一种广泛运用的在应用程序之间传输信息的机制 * * BroadcastReceiver(广播接收者) * 是对发送出来的广播进行过滤接收并响应的一类组件,它就是用来接收来自系统和应用中的广播 * 使用方法: * 发送:把信息装入一个intent对象(action,category) * 通过调用相应方法将intent对象以广播形式发送出去 * sendBroadcast() * sendOrdercast() * sendStickyBroadcast() * 注意!!! * BroadcastReceiver生命周期只有十秒左右, * 如果在onReceiver内做超过十秒内的事情,就会报错 * 应该通过发送Intent给service,由service来完成 * 不能用子线程 * *广播的种类: * 普通广播,有序广播,异步广播(不能讲处理结果传给下一个接收者,无法终止广播) * 普通广播的特点: * -同级别接收先后是随机的(无序) * -级别低的后收到广播(priority) * -接收器不能截断广播的传播也不能处理广播,否则报错 * -同级别动态注册高于静态注册 * * 有序广播的特点: * -同级别接收顺序是随机的 * -能截断广播的继续传播,高级别的广播接收器收到该广播后,可以决定把该广播是否截断 * -接收器能截断广播的继续传播,也能处理广播 * -同级别动态注册高于静态注册 * *静态注册:在manifest里面注册(与activity类似) * <receiver android:name="com.examle.android_broadcast.MyBroadcastReceiver1"> <intent-filter android:priority="200" > <action android:name="broadcast_one"/> </intent-filter> </receiver> android:priority="200" 优先级,范围-1000到1000 动态注册:在MainActivity中注册, IntentFilter filter = new IntentFilter("broadcast_one");//创建过滤器,过滤动作为broadcast_one的广播 MyBroadcastReceiver1 receiver = new MyBroadcastReceiver1();//创建广播接收对象 registerReceiver(receiver, filter);//注册广播 !!!注意:动态注册广播后需要在广播接收后关闭广播(重写onDestory方法) * @author Administrator * */ public class MainActivity extends Activity { private MyBroadcastReceiver3 receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*IntentFilter filter = new IntentFilter("broadcast_one");//创建过滤器,过滤动作为broadcast_one的广播 MyBroadcastReceiver1 receiver = new MyBroadcastReceiver1();//创建广播接收对象 registerReceiver(receiver, filter);//注册广播 */ } public void doClick(View view){ switch(view.getId()){ case R.id.btn_1://发送一条普通广播 Intent intent = new Intent(); intent.putExtra("msg", "这是一条普通广播");//设置发送的信息 intent.setAction("broadcast_one");//设置发送的动作 sendBroadcast(intent);//发送广播 break; case R.id.btn_2://发送一条有序广播 Intent intent2 = new Intent(); intent2.putExtra("msg", "这是一条有序广播"); intent2.setAction("broadcast_one");//跳转的动作 sendOrderedBroadcast(intent2, null); break; case R.id.btn_3://发送一条异步广播, //异步广播不能终止也就是会每次都存在,用先发送后注册来说明,点击n次出现n条广播 unregisterReceiver(receiver);该方法对其并不起作用 Intent intent3 = new Intent(); intent3.putExtra("msg", "这是一条异步广播"); intent3.setAction("broadcast_three"); sendStickyBroadcast(intent3); IntentFilter filter = new IntentFilter("broadcast_three"); receiver = new MyBroadcastReceiver3(); registerReceiver(receiver, filter); break; } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //unregisterReceiver(receiver); } }
MyBroadcastReceiver1:
package com.example.android_broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class MyBroadcastReceiver1 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String s = intent.getStringExtra("msg"); System.out.println("broadcast_one接收消息:"+s); //有序广播中,可以对收到的数据进行截断或处理 //abortBroadcast();//截断广播,如果是普通广播,就会失败 Bundle bundle = new Bundle(); bundle.putString("newMsg", "数据被广播1处理过-->"+s); setResultExtras(bundle); } }
MyBroadcastReceiver2:
package com.example.android_broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class MyBroadcastReceiver2 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String s = intent.getStringExtra("msg"); System.out.println("broadcast_two接收消息:"+s); Bundle bundle = getResultExtras(true); String s2 = bundle.getString("newMsg"); System.out.println("broadcastreceiver2得到的结果是:"+s2); } }
MyBroadcastReceiver3:
package com.example.android_broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadcastReceiver3 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String s = intent.getStringExtra("msg"); System.out.println("broadcast_three接收消息:"+s); //abortBroadcast();//截断广播,因为是普通广播,所以会失败 } }
以上是关于Android_Broadcast的主要内容,如果未能解决你的问题,请参考以下文章