• Android 自定义广播刷新页面数据信息


    1.定义一个广播的接收类

        public class MyReceivers extends BroadcastReceiver {
            @SuppressLint("WrongConstant")
            @Override
            public void onReceive(Context context, Intent intent) {
                String json = intent.getExtras().getString("json");
                if (json == null){
                    LoadDialog.showDialogLoad(CommunicateActivity.this, getResources().getString(R.string.loading_progress_text));
                }else {
                    LoadDialog.dismissProgressDialog();
                    sharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
                    jsons = sharedPreferences.getString("json", "");
                    initSelectPhone();
                    getCache();
                    initCommunicte();
                    serch();
                    getAllContacts();
                    removeHeadData();
                }
                android.widget.Toast.makeText(context, "广播来啦", Toast.LENGTH_SHORT).show();
            }
        }
    

     2.注册广播(可以是静态注册——清单文件中注册)/也可以是动态注册——代码中注册)

    //静态注册广播
      <receiver
                android:name=".MyReceivers"
                android:enabled="true"
                android:exported="false"
                android:permission="FinishActivity">
     </receiver>
    
      //动态注册一个自定义的广播,Action为FinishActivity
            MyReceivers myReceivers = new MyReceivers();
            registerReceiver(myReceivers, new IntentFilter("FinishActivity"));
    

    3.发送自定义广播即可实现

          Intent intent = new Intent("FinishActivity");
          intent.putExtra("json",jsonStr);
          context.sendBroadcast(intent);//发送对应的广播
    
  • 相关阅读:
    JVM
    OLAP
    rocketMq学习
    redis的使用小记
    CRT配置端口转发
    冒泡排序
    spring AOP-切面编程
    linux下对jar包和war包进行重新打包
    oracle-sql性能优化
    遍历List,根据子项的某个属性分组
  • 原文地址:https://www.cnblogs.com/monkey0928/p/13070698.html
Copyright © 2020-2023  润新知