Update android Textview continuously

前端 未结 4 1053
执笔经年
执笔经年 2021-01-22 12:27

I am working on an Android Application which have an one activity class and service class. In service, Continuous bulk data (1090 by

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 12:46

    If you want to use Schedule and timer task then you can See My Answer

    To solve current issue follow this bellow instructions. Suppose your activity has a Broadcast Receiver

    private BroadcastReceiver mReceiver;
    

    Then you override methods onResume() where your broadcast receiver will be registered and also onPause() where will your receiver be unregistered:

    @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
    
            IntentFilter intentFilter = new IntentFilter(
                    "android.intent.action.MAIN");
    
            mReceiver = new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context context, Intent intent) {
                    //extract your message from intent
                    String msg_for_me = intent.getStringExtra("YOUR_MESSAGE");
                    //log your message value
                    Log.i("MyTag", msg_for_me);
    
                }
            };
            //registering your receiver
            this.registerReceiver(mReceiver, intentFilter);
        }
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            //unregister your receiver
            this.unregisterReceiver(this.mReceiver);
        }
    

    Here the broadcast receiver is filtered via android.intent.action.MAIN and from Service the message will BroadCast using this filter

    Now your Method Receivepatientattributes will like this :

    public void Receivepatientattributes(byte[] readBuf, int len) {
    
        String total_data = "";
        total_data = bytetohex(readBuf, len);
    
        Intent i = new Intent("android.intent.action.MAIN").putExtra("YOUR_MESSAGE",  total_data);
        this.sendBroadcast(i);
    
    }
    

    Thats it. :)

提交回复
热议问题