Android - BroadcastReceiver 广播

Broadcast被分成两种:

Normal Broastcast 普通广播 Ordered Broadcast 有序广播
在逻辑上同一时刻可以被所有接收者接收,消息传递效率高。缺点接收者不能将处理结果传给下个接收者,且无法终止传播。 按声明优先级依次接收。优先级元素的android:priority属性中,数大优先高,取值为-1000-1000,也可调用IntentFilter对象setPriority()设置。可以终止传播,一旦终止,后面的接收者就无法接收。另外Ordered Broadcast的接收者可以将数据传递给下一个接收者。
Context提供两个方法用于发送广播。
sendBroadcast():发送Normal Broadcast 普通广播。
sendOrderedBroadcast():发送Ordered Broadcast 有序广播。
对于Ordered Broadcast,系统根据接收者优先级按顺序逐个执行接收,优先接收到Broadcast的接收者可以调用BroadcastReceiver的abortBroadcast()方法终止Broadcast。如果被终止,后面的接收者就无法接收到Broadcast。
对于Ordered来说,优先接收到Broadcast的接收者可以通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,然后传给下一个接收者,下一个接收者通过:Bundle bundle = getResultExtras(true)可以获取上一个接收者存入的数据。


普通广播

静态注册:

AndroidManifest.xml

<receiver android:name="com.example.venbo.MyReceive">
   <intent-filter>
       <action android:name="FM895">
       action>
   intent-filter>
receiver>

我们采用动态注册

动态注册:

编写主类:实例化IntentFilter对象,添加.addAction,注册广播接收
编写按钮点击事件:实例化Intent,设置.setAction,.putExtra放内容,sendBroadcast发送
编写接收类,触发Toast提示;

Java主类:

public class MainActivity extends Activity {

	Button button_send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        		//实例化IntentFilter对象
                 IntentFilter filter = new IntentFilter();
                 filter.addAction("FM895");
                 MyReceive myReceive = new MyReceive();
                 //注册广播接收
                registerReceiver(myReceive,filter);
        
        button_send = (Button) findViewById(R.id.button_send);
        button_send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
					Intent intent = new Intent();
					intent.setAction("FM895");
					intent.putExtra("information","雷霆嘎巴!");
					sendBroadcast(intent);
			}
		});
    }
}

接收类:

public class MyReceive extends BroadcastReceiver {
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		Toast t = Toast.makeText(arg0,"动态广播:"+ arg1.getStringExtra("information"), Toast.LENGTH_SHORT);
		t.setGravity(Gravity.TOP,0,0);
		t.show();
	}
}

点击运行 结果:
Android - BroadcastReceiver 广播_第1张图片

有序广播

主代码:

public class MainActivity extends Activity {

	Button button_send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        		//实例化IntentFilter对象
                 IntentFilter filter = new IntentFilter();
                 filter.addAction("FM895");
                 filter.setPriority(1000);			//__________设置优先级
                 MyReceive myReceive = new MyReceive();
                 //注册广播接收
                 registerReceiver(myReceive,filter);
                
                IntentFilter filter2 = new IntentFilter();
                filter.addAction("FM895");
                filter.setPriority(999);
                MyReceive2 myReceive2 = new MyReceive2();
                //注册广播接收
                registerReceiver(myReceive2,filter);
        
        button_send = (Button) findViewById(R.id.button_send);
        button_send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
					Intent intent = new Intent();
					intent.setAction("FM895");
					intent.putExtra("information","雷霆嘎巴!");
					//sendBroadcast(intent);  //发送普通广播
					sendOrderedBroadcast(intent,null); 		//————————这次发送有序广播
			}
		});
    }

}

接受者1:

public class MyReceive extends BroadcastReceiver {
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		Toast t = Toast.makeText(arg0,"动态广播:"+ arg1.getStringExtra("information"), Toast.LENGTH_SHORT);
		t.setGravity(Gravity.TOP,0,0);		//将Toast设置在屏幕顶端
		t.show();
		//abortBroadcast();				//调用广播将被截断,后续接受者无法接受,结果只有图一
	}
}

接受者2:

public class MyReceive2 extends BroadcastReceiver {
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		Toast t = Toast.makeText(arg0,"动态广播:"+ arg1.getStringExtra("information"), Toast.LENGTH_SHORT);
		t.setGravity(Gravity.CENTER,0,0);		//将Toast设置在屏幕中间
		t.show();
	}
}

结果:
图1
Android - BroadcastReceiver 广播_第2张图片
两秒后 图2:
Android - BroadcastReceiver 广播_第3张图片

自己动手试一试,你也可以!

你可能感兴趣的:(Android,java,安卓,android,android,studio)