安卓BroadcastReceiver组件使用系列2:给多个广播接收者发送广播和有序广播的使用

给多个广播接收者发送广播、有序广播的使用在安卓开发中是经常使用的方式,下面我们来介绍一下它的使用方法。

整体思路:在xml文件中放置两个Button控件,给这两个Button控件设置点击事件,在第一个点击事件中传递一个数据,设置一个动作并发送广播,在第二个点击事件中传递一个数据,设置一个动作并发送有序广播。新建三个继承BroadcastReceiver的广播接收类,用于接收传递的数据。在第二个BroadcastReceiver子类中停止广播的接收,发现在发送有序广播的时候会影响后续广播接收者进行接收广播。在清单文件注册者三个广播接收类,并设置它们的优先级和行为。

activity_main.xml文件:



    
Broadcast1.java文件:

package com.example.android_broadcast_order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Broadcast1 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
        System.out.println("Broadcast1-->"+intent.getStringExtra("name"));
	}

}
Broadcast2.java文件:

package com.example.android_broadcast_order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Broadcast2 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		 System.out.println("Broadcast2-->"+intent.getStringExtra("name"));
		 abortBroadcast();//结束广播,如果是有序广播的话则不继续发送后面的广播
		 }

}
Broadcast3.java文件:
package com.example.android_broadcast_order;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Broadcast3 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("Broadcast3-->"+intent.getStringExtra("name"));
	}

}
MainActivity.java文件:

package com.example.android_broadcast_order;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;
	private Button button2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)findViewById(R.id.button1);
		button2=(Button)findViewById(R.id.button2);
		
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				给多个广播接收者发送广播
				Intent intent=new Intent();
				intent.putExtra("name", "jack");
				intent.setAction("com.mybroadcast.sender");//设置一个动作
				sendBroadcast(intent);
			}
		});
		
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.putExtra("name", "rose");
				intent.setAction("com.mybroadcast.sender");//设置一个动作
//				发送有序广播
				sendOrderedBroadcast(intent, null);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
AndroidManifest.xml文件:

 
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        






你可能感兴趣的:(移动端)