How to update the UI of Activity from BroadCastReceiver

前端 未结 6 563
礼貌的吻别
礼貌的吻别 2020-12-03 22:57

I am learning Android concepts Activity and BroadCastReceiver. I want to update the content of Activity from the Br

相关标签:
6条回答
  • 2020-12-03 23:34

    Yes its possible. This is what i do. Class i send the broadcast from (BackgroundActivity.java):

    public static final String BROADCAST_BUFFER_SEND_CODE = "com.example.SEND_CODE";
    
    onCreate(){
       bufferIntentSendCode = new Intent(BROADCAST_BUFFER_SEND_CODE);
    }
    
    private void sendBufferingBroadcastSendCode() {
       bufferIntentSendCode.putExtra("buffering", "1");
       sendBroadcast(bufferIntentSendCode);
    }
    

    The class it will receive the broadcast(SendCode.java):

    onResume(){
            registerReceiver(broadcastBufferReceiver, new IntentFilter(BackgroundActivity.BROADCAST_BUFFER_SEND_CODE));
    }
    
    // set up broadcast receiver
    private BroadcastReceiver broadcastBufferReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent bufferIntent) {
            SendCode.this.LoadMessages(alarmNumber);
        }
    };
    

    I unregister it in onPause

    this.unregisterReceiver(broadcastBufferReceiver);
    
    0 讨论(0)
  • 2020-12-03 23:39

    Register a new BroadcastReceiver object in your activity with same intent-filters as your MyBroadtCastReceiver. Since BroadcastReceiver and MyBroadtCastReceiver has same intent-filters both of their onReceive() will be invoked. Whatever update that you want to do in Activity can be done in onReceive of your BroadcastReceiver.

    0 讨论(0)
  • 2020-12-03 23:39

    Squonk-s answer only works, if the Activity is active currently. If you dont want to declare / define your BroadcastReceiver (BR) in an other Activity, or if you want to make some changes even if the app is not foreground, than your solution would look something like this.

    First, you declare the BR, and save, or override the data needed to show in Acitvity.

    public class MyBR extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // override the data. Eg: save to SharedPref
        }
    }
    

    Then in Activity, you show the data

    TextView tv = findViewById(R.id.tv);
    tv.setText(/*get the data Eg: from SharedPref*/);
    

    And you should use a Timer to refresh the tv as well:

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                     public void run() {
                        TextView tv = findViewById(R.id.tv);
                        tv.setText(/*get the data Eg: from SharedPref*/);
                    }
                });
            }
        }, REFRESH_RATE, REFRESH_RATE);
    

    REFRESH_RATE could be something like 1 second, but you decide.

    0 讨论(0)
  • 2020-12-03 23:48

    try like this it may help you.

    Define this method in activity's oncreate method in which you want to update ui,

        BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        //your code to update ui
                    }
                };
       LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("giveyourappname"));
    

    Define this action at place from where you want to update ui,

    try{
            ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
            List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            Log.d("Activity", "Current Activity ::" + taskInfo.get(0).topActivity.getClassName());
            Log.d("Package", "Package Name :  "+ componentInfo.getPackageName());
    
            if(componentInfo.getPackageName().equals("your application package name")){
                 Intent intent = new Intent("giveyourappname");
                    //add data you wnat to pass in intent
                    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            }
        }catch(Throwable e){
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-12-03 23:49
    You can do like this:
    
    public class MyActivity extends Activity{
    
    // used to listen for intents which are sent after a task was
    // successfully processed
    private BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            new UpdateUiTask().execute();
        }
    };
    
    @Override
    public void onResume() {        
        registerReceiver(mUpdateReceiver, new IntentFilter(
                YOUR_INTENT_ACTION));
        super.onResume();
    }
    
    @Override
    public void onPause() {     
        unregisterReceiver(mUpdateReceiver);
        super.onPause();
    }
    
    
    // used to update the UI
    private class UpdateUiTask extends AsyncTask<Void, Void, String> {
    
        @Override
        protected void onPreExecute() {
    
        }
    
        @Override
        protected String doInBackground(Void... voids) {
            Context context = getApplicationContext();
            String result = "test";
            // Put the data obtained after background task. 
            return result;
        }
    
        @Override
        protected void onPostExecute(String result) {
            // TODO: UI update          
        }
    }  
    
    }
    
    0 讨论(0)
  • 2020-12-03 23:54

    A BroadcastReceiver can be used in many ways but when it comes to something as specific as updating the UI components of an Activity, there is little advantage to declaring / defining a BroadcastReceiver in it's own Java class file.

    Reasoning - the BroadcastReceiver has to have some prior "knowledge" of the Activity and what it is required to do in order to update the UI. In effect the BroadcastReceiver is tied to the Activity itself and it makes sense to declare / define it as an inner class.

    Another important aspect is the Activity needs to be in a "running" (i.e., visible) state in order to guarantee manipulation of UI components. In this case, registering the receiver in onResume() and unregistering in onPause() will help prevent problems.

    Using a generic template I'd do something like the following...

    class MyActivity extends Activity {
    
        boolean mIsReceiverRegistered = false;
        MyBroadcastReceiver mReceiver = null;
    
        // onCreate(...) here
    
        @Override
        protected void onResume() {
    
            // Other onResume() code here
    
            if (!mIsReceiverRegistered) {
                if (mReceiver == null)
                    mReceiver = new MyBroadcastReceiver();
                registerReceiver(mReceiver, new IntentFilter("YourIntentAction"));
                mIsReceiverRegistered = true;
            }
        }
    
        @Override    
        protected void onPause() {
            if (mIsReceiverRegistered) {
                unregisterReceiver(mReceiver);
                mReceiver = null;
                mIsReceiverRegistered = false;
            }
    
            // Other onPause() code here
    
        }
    
        private void updateUI(Intent intent) {
            // Do what you need to do
        }
    
        private class MyBroadcastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                updateUI(intent);
            }
        }
    }
    

    EDIT: A couple of extra notes...

    1. The life-cycle of a BroadcastReceiver is between entering and leaving onReceive(...). Once it has returned from onReceive(...) the instance remains in a dormant state waiting for the next broadcast.
    2. Directly related to point 1 - a BroadcastReceiver isn't designed for "heavy lifting". Basically the onReceive(...) method should be kept as simple as possible. Any methods it calls should also be as light-weight as possible...get in, do your stuff, get out then wait for the next broadcast. If updating the UI is going to take some time (perhaps updating a ListView by re-querying a database for a large amount of data for example), consider calling code which performs asynchronously (an AsyncTask for example).
    0 讨论(0)
提交回复
热议问题