How to update the UI of Activity from BroadCastReceiver

前端 未结 6 566
礼貌的吻别
礼貌的吻别 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);
    

提交回复
热议问题