analysis
Normal broadcasting (Normal Broadcast):
First, the advantages and disadvantages: advantages and disadvantages and orderly broadcast opposite!
Second, the method of transmitting broadcasting: sendBroadcast ()
Ordered Broadcasting (Ordered Broadcast):
A, disadvantages
advantages:
1, different priority, priority Receiver may process the data, and passed to the next Receiver
2, may be terminated by the spread broadcasting abortBroadcast
Disadvantages: inefficient
two, method of transmitting broadcast: sendOrderedBroadcast ()
PRIORITY Broadcast Receiver may be received (the Bundle) method stores the processing result by Broadcast setResultExtras,
next acquired by the Receiver Bundle bundle = getResultExtras (true) Method the data came on a Receiver
Program effect: Click, two Receiver receives the same broadcasting, print out the data in the logcat (Receiver according to a priority order, Receiver2 first, after receiver1)
Code
1 Registration broadcasting
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.song"
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:label="@string/app_name"
android:name=".C48_BroadcastActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--优先级的设定 MyReceiver2大于MyReceiver1,优先级的范围-1000~1000 -->
</activity>
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="200">
<action android:name="com.song.123"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="1000">
<action android:name="com.song.123"/>
</intent-filter>
</receiver>
</application>
</manifest>
2 receives a broadcast
package com.song;
//优先接到广播,bundle绑上key为b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MyReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receiver2");
// context.getSystemService(name);
Bundle bundle=intent.getExtras();
bundle.putString("b", "bbb");
System.out.println("a="+bundle.get("a"));
setResultExtras(bundle);
//切断广播
// abortBroadcast();
}
}
package com.song;
//接收从receiver2传来的广播,包含key为a和b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MyReceiver1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receiver1");
//要不要接受上一个广播接收器receiver2传来的的数据
Bundle bundle=getResultExtras(true);
System.out.println("a="+bundle.getString("a")+",b="+bundle.getString("b"));
}
}
3 Send broadcast
package com.song;
//发送广播,bundle绑上key为a的数据
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class C48_BroadcastActivity extends Activity {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.song.123");
Bundle bundle=new Bundle();
bundle.putString("a", "aaa");
intent.putExtras(bundle);
//有序广播
sendOrderedBroadcast(intent, null);
}
});
}
}
4 Kills broadcast
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(localReceiver);
}