美文网首页
android第一行代码笔记9-Broadcast Receiv

android第一行代码笔记9-Broadcast Receiv

作者: 刚刚8888 | 来源:发表于2020-06-30 18:20 被阅读0次
    • 标准广播(Normal broadcasts)是一种完全异步执行的广播


      企业微信截图_713ab319-484b-4c42-9ef3-50bdfc695188.png
    • 有序广播(Ordered broadcasts)则是一种同步执行的广播,在广播发出之后,同一时刻 只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广 播才会继续传递。


      企业微信截图_1f28e1c9-fb33-4433-a9ba-ed8ccf032a3c.png
    
     public class MainActivity extends Activity {
            private IntentFilter intentFilter;
            private NetworkChangeReceiver networkChangeReceiver;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
    intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter);
    }
            @Override
            protected void onDestroy() {
                super.onDestroy();
                unregisterReceiver(networkChangeReceiver);
            }
     class NetworkChangeReceiver extends BroadcastReceiver {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Toast.makeText(context, "network changes",
        Toast.LENGTH_SHORT).show();
    } }
    }
    

    相关文章

      网友评论

          本文标题:android第一行代码笔记9-Broadcast Receiv

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