http://blog.csdn.net/xiazdong/article/details/7768807
广播接收者核心代码
同步广播发送方核心代码:
- Intent intent = new Intent();
- intent.setAction("...");
- Context.sendBroadcast(intent);
有序广播发送方核心代码:
- Intent intent = new Intent();
- intent.setAction("...");
- Context.sendOrderedBroadcast(intent,null);
广播接收者核心代码:
- public class Receiver extends BroadcastReceiver{
- public void onReceive(Context context, Intent intent) {
- Bundle bundle = intent.getExtras();
- ...
- }
- }
AndroidManifest.xml
- <application>
- <receiver android:name=".Receiver">
- <intent-filter android:priority="1000">
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>
- </application>
三、广播实例
1.同步广播实例
场景说明:
(1)广播发送者:
- package com.xiazdong.broadcastsender;
- 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;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Button button;
- private OnClickListener listener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("com.xiazdong");
- intent.putExtra("name", "xiazdong");
- MainActivity.this.sendBroadcast(intent);
- Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)this.findViewById(R.id.button);
- button.setOnClickListener(listener);
- }
- }
(2)广播接收者
- package com.xiazdong.broadcastreceiver1;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String name = intent.getExtras().getString("name");
- Log.i("Recevier1", "接收到:"+name);
- }
- }
AndroidManifest.xml
- <receiver android:name=".Receiver">
- <intent-filter>
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>
结果:
2.有序广播实例
场景说明:
(1)广播发送者
- package com.xiazdong.broadcastsender;
- 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;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Button button;
- private OnClickListener listener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction("com.xiazdong");
- intent.putExtra("name", "xiazdong");
- MainActivity.this.sendOrderedBroadcast(intent, null); //有序广播发送
- Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)this.findViewById(R.id.button);
- button.setOnClickListener(listener);
- }
- }
(2)广播接收者
Receiver1
- package com.xiazdong.broadcastreceiver1;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String name = intent.getExtras().getString("name");
- Log.i("Recevier1", "接收到:"+name);
- abortBroadcast(); //Receiver1接收到广播后中断广播
- }
- }
AndroidManifest.xml
- <receiver android:name=".Receiver">
- <intent-filter android:priority="1000"> <!-- 设置最高优先级 -->
- <action android:name="com.xiazdong"/>
- </intent-filter>
- </receiver>