zoukankan      html  css  js  c++  java
  • 有序广播

    package com.example.lenovo.guangbo;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void bt_1(View v)
        {
            //发送一般广播
            //使用action属性指定广播频率
            Intent intent=new Intent("com.hanqi.gunagbo.test2");
            intent.putExtra("name","广播发送测试");
            //发送
            sendBroadcast(intent);
            Toast.makeText(MainActivity.this, "一般广播已发出", Toast.LENGTH_SHORT).show();
        }
    
        MyReceiver myReceiver;
        //动态注册
        public void bt_2(View v)
        {
            myReceiver=new MyReceiver();
            IntentFilter intentFilter = new IntentFilter("com.hanqi.guangbo.test2");
            intentFilter.setPriority(100);
            registerReceiver(myReceiver, intentFilter);
        }
        public void bt_3(View v)
        {
            //发送有序广播
            Intent intent=new Intent("com.hanqi.gunagbo.test1");
            intent.putExtra("name","广播发送测试");
            //发送
            sendOrderedBroadcast(intent, null);
            Toast.makeText(MainActivity.this, "有序广播已发出", Toast.LENGTH_SHORT).show();
    
        }
    
        //反注册,onDestroy()
        @Override
        protected void onDestroy() {
    
            if (myReceiver!=null) {
                unregisterReceiver(myReceiver);
            }
            super.onDestroy();
        }
    }
    MainActivity.java

    优先级

    package com.example.lenovo.guangbo;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MyReceiver extends BroadcastReceiver {
        public MyReceiver() {
            Log.e("TAG","MyReceiver被实例化");
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (isOrderedBroadcast()) {
                //阻断
                abortBroadcast();
            }
           //
            String str=intent.getStringExtra("name");
            intent.putExtra("name1","这是被修改的内容");
            //再重新发送
            //context.sendOrderedBroadcast(intent,null);
            Toast.makeText(context, "MyReceiver1收到的广播:"+str, Toast.LENGTH_SHORT).show();
        }
    }
    MyReceive
    package com.example.lenovo.guangbo;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyReceiver2 extends BroadcastReceiver {
        public MyReceiver2() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String str=intent.getStringExtra("name1");
            Toast.makeText(context, "MyReceiver2收到的广播:" + str, Toast.LENGTH_SHORT).show();
    
        }
    }
    MyReceive

    静态优先级

    <intent-filter android:priority="1000">

    动态优先级
    intentFilter.setPriority(100);



     
  • 相关阅读:
    (004)maven执行junit单元测试,指定执行哪些测试类
    (009)Nginx静态资源web服务
    (008)Nginx的访问控制_介绍实现访问控制的基本方式
    (03)Nginx将配置文件default.conf重命名后报Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.解决方案
    (007)Nginx的请求限制_配置语法与原理
    (006)Nginx之模块讲解
    (005)Nginx之日志log_format
    (004)Nginx默认配置语法解析及演示
    (003)Nginx编译配置参数讲解
    Gym
  • 原文地址:https://www.cnblogs.com/1ming/p/5626091.html
Copyright © 2011-2022 走看看