美文网首页
发送自定义广播

发送自定义广播

作者: TTTqiu | 来源:发表于2016-04-22 15:13 被阅读66次

    一、发送标准广播


    Intent intent = new Intent("com.example.broadcasttest. MY_BROADCAST");
    sendBroadcast(intent);
    

    二、发送有序广播


    Intent intent = new Intent("com.example.broadcasttest. MY_BROADCAST");
    sendOrderedBroadcast(intent, null);
    
    截断广播:
    public class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received in MyBroadcastReceive", Toast.LENGTH_SHORT).show();
            abortBroadcast();
        }
    }
    
    abortBroadcast();
    
    • 截断后,后续的接收器就不会接收到广播了。
    • 接收广播的先后顺序:优先级 android:priority:
    <receiver android:name=".MyBroadcastReceiver">
        <intent-filter android:priority="100" >
            <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
        </intent-filter>
    </receiver>
    

    三、本地广播


    • 前面我们发送和接收的广播全部都是属于系统全局广播,即发出的广播可以被其他任何的任何应用程序接收到,并且我们也可以接收来自于其他任何应用程序的广播。这样就很容易会引起安全性的问题。
    • Android 本地广播机制,使用这个机制发出的广播只能够在应用程序的内部进行传递,并且广播接收器也只能接收来自本应用程序发出的广播,这样所有的安全性问题就都不存在了。
    • 本地广播是无法通过静态注册的方式来接收的。因为要用时程序肯定已经启动了。
    public class MainActivity extends Activity {
        private IntentFilter intentFilter;
        private LocalReceiver localReceiver;
        private LocalBroadcastManager localBroadcastManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            localBroadcastManager = LocalBroadcastManager.getInstance(this);  // 获取实例
    
            Button button = (Button) findViewById(R.id.button);
            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, "received local broadcast", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例
    
    Intent intent = new Intent("com.example.broadcasttest. LOCAL_BROADCAST");
    localBroadcastManager.sendBroadcast(intent); // 发送本地广播
    
    localBroadcastManager.unregisterReceiver(localReceiver);
    
    intentFilter = new IntentFilter(); 
    intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST"); 
    localReceiver = new LocalReceiver(); 
    localBroadcastManager.registerReceiver(localReceiver, intentFilter); // 注册本地广播监听器
    

    相关文章

      网友评论

          本文标题:发送自定义广播

          本文链接:https://www.haomeiwen.com/subject/qhrxrttx.html