• Android 使用BroadcastReceiver的几种方法


    发送自定义广播

    全局广播

    • 发送标准广播

    1.定义广播接收器.(在发送广播前,需要先定义一个广播接收器,不然发了也是白发)

    public class MyBroadcastReceiver extends BroadcastReceiver {
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		Toast.makeText(context, "MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
    	}
    }
    

    2.在AndroidManifest.xml里面注册.这里我们会让我们定义好的广播接收器接收一条为"com.example.broadcasttest.MY_BROADCAST"的自定义广播.

    <receiver      android:name="com.example.broadcasttest.MyBroadcastReceiver" >
                <intent-filter >
                    <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
                </intent-filter>
            </receiver>
    

    3.然后我们写一个触发事件手动的把广播发出去.

    button.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
    				sendBroadcast(intent);
    			}
    		});
    

    1.新建一个广播接收器.

    public class MyBroadcastReceiver extends BroadcastReceiver {
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		Toast.makeText(context, "MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
    	}
    }
    

    2.我们在AndroidManifest.xml文件对广播进行注册.

    <receiver          android:name="com.example.broadcasttest.MyBroadcastReceiver" >
                <intent-filter >
                    <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
                </intent-filter>
            </receiver>
    

    3.然后我们写一个触发事件手动的把广播发出去.

    button.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");		sendOrderedBroadcast(intent, null);
    			}
    		});
    

    看到这里一定有人让位标准广播和有序广播是相同的,其实不是.sendOrderedBroadcast(intent, null);"这里我们的方法该成了sendOrderedBroadcast();还有不要忘记了这个时候我们发送的是有序广播,有序广播是可以截断的.优先级高的广播接收器可以截断广播,以阻止其继续传播.

    • 设置广播接收器的优先级,android:priority="";
    <receiver 
     android:name="com.example.broadcasttest.MyBroadcastReceiver" >
                <intent-filter android:priority="100">
                    <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
                </intent-filter>
            </receiver>
    
    • 截断广播
    @Override
    	public void onReceive(Context context, Intent intent) {
    		abortBroadcast();	//截断广播
    	}
    

    使用本地广播

    
    public class MainActivity extends Activity {
    	private IntentFilter intentFilter;
    	private Button button;
    	private LocalBroadcastManager localBroadcastManager;
    	private LocalReceiver localReceiver;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.activity_main);
    		localBroadcastManager=LocalBroadcastManager.getInstance(this);	//获取实例
    		button=(Button) findViewById(R.id.btn);
    		button.setOnClickListener(new OnClickListener() {
    		
    			@Override
    			public void onClick(View v) {
    				Intent intent=new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
    				localBroadcastManager.sendBroadcast(intent);	//发送本地广播
    			}
    		});
    		intentFilter=new IntentFilter();
    		intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
    		localReceiver=new LocalReceiver();
    		localBroadcastManager.registerReceiver(localReceiver, intentFilter);	//注册本地广播监听器
    	}
    	
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		localBroadcastManager.unregisterReceiver(localReceiver);
    	}
    	
    	
    	class LocalReceiver extends BroadcastReceiver{
    	
    		@Override
    		public void onReceive(Context context, Intent intent) {
    			Toast.makeText(context, "1", Toast.LENGTH_LONG).show();
    		}
    	}
    }
    
    

    可以看到他和我们的上面提到的差不多.只不过现在是首先通过LocalBroadcastManager的getInstance(this);得到它的实例,然后在注册广播接收器的时候调用的是localBroadcastManager的registerReceiver();方法.在发送广播的时候调用的是localBroadcastManager的sendBroadcast(intent);方法.

  • 相关阅读:
    B
    A
    P1057 传球游戏
    P1702 突击考试
    P1394 山上的国度
    P2117 小Z的矩阵
    P1510 精卫填海
    P1294 高手去散步
    P1071 潜伏者
    保留
  • 原文地址:https://www.cnblogs.com/stareblankly/p/5076354.html
Copyright © 2020-2023  润新知