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.注册广播(可以是静态注册——清单文件中注册)/也可以是动态注册——代码中注册)

 

//静态注册广播
  
 

 

  //动态注册一个自定义的广播,Action为FinishActivity
        MyReceivers myReceivers = new MyReceivers();
        registerReceiver(myReceivers, new IntentFilter("FinishActivity"));

 

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

      Intent intent = new Intent("FinishActivity");
      intent.putExtra("json",jsonStr);
      context.sendBroadcast(intent);//发送对应的广播

 

你可能感兴趣的:(Android 自定义广播刷新页面数据信息)