本文介绍了不断更新的Andr​​oid的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,它有一个活动类和服务类。在服务,连续本体数据(1090字节)将在收到每10毫秒。我需要用这些大量的数据不断更新文本视图。什么是推荐的方法来更新文字从一个连续的后台服务?

服务类

 公共类RecepService延伸服务{

公共静态处理程序mHandler;
StringBuilder的十六进制串;

@覆盖
公众的IBinder onBind(意向意图){
    返回null;
}

@覆盖
公共无效的onCreate(){
    super.onCreate();

    在里面();
}

私人无效的init(){

    mHandler =新的处理程序(){
        @覆盖
        公共无效的handleMessage(信息MSG){
            如果(msg.what ==为0x123){

                字节[]的ReadBuf =(字节[])msg.obj;
                INT readBuflen = msg.arg1;

                //这里将接收1090个字节的数据
                //每10毫秒
                Receivepatientattributes(的ReadBuf,readBuflen);
            }
        }
    };
}

公共无效Receivepatientattributes(byte []的的ReadBuf,INT LEN){

    字符串total_data =;
    total_data = bytetohex(的ReadBuf,LEN);
    MainActivity.recep.setText(MainActivity.recep.getText()的toString()+\ t+
            + total_data);
}

字符串bytetohex(byte []的TXT,INT LEN){

    串P =;
    byte []的文字=新的字节[长度];
    文= TXT;

    十六进制串=新的StringBuilder();
    对于(INT J = 0; J< LEN; J ++){

        字符串十六进制= Integer.toHexString(0xFF的&放大器; TXT [J]);

        如果(hex.length()== 1){
            hexstring.append(0);
        }

        hexstring.append(十六进制+);

    }

    p值= P + hexstring.toString();
    返回磷;
}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
}

@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){

    返回START_STICKY;
}
}
 

解决方案

如果你想使用计划和定时器的任务,那么你可以见我的回答

要解决当前的问题,遵循这一波纹管的说明。假如你的活动有一个广播接收器

 私人BroadcastReceiver的mReceiver;
 

然后重写方法 onResume(),你的广播接收器将被注册,并的onPause(),其中会您的接收器被注销:

  @覆盖
    保护无效onResume(){
        // TODO自动生成方法存根
        super.onResume();

        IntentFilter的IntentFilter的=新的IntentFilter(
                android.intent.action.MAIN);

        mReceiver =新的BroadcastReceiver(){

            @覆盖
            公共无效的onReceive(上下文的背景下,意图意图){
                //提取意向留言
                串msg_for_me = intent.getStringExtra(YOUR_MESSAGE);
                //记录你的信息的价值
                Log.i(MyTag的,msg_for_me);

            }
        };
        //注册您的接收器
        this.registerReceiver(mReceiver,IntentFilter的);
    }

    @覆盖
    保护无效的onPause(){
        // TODO自动生成方法存根
        super.onPause();
        //注销你的接收机
        this.unregisterReceiver(this.mReceiver);
    }
 

下面的广播接收机通过 android.intent.action.MAIN 过滤,并从服务的消息将广播使用此过滤器

现在你的方法 Receivepatientattributes 将是这样的:

 公共无效Receivepatientattributes(byte []的的ReadBuf,INT LEN){

    字符串total_data =;
    total_data = bytetohex(的ReadBuf,LEN);

    意图I =新的意向书(android.intent.action.MAIN)putExtra(YOUR_MESSAGE,total_data)。
    this.sendBroadcast(ⅰ);

}
 

这就是它。 :)

I am working on an Android Application which have an one activity class and service class. In service, Continuous bulk data (1090 bytes) will be received every 10 milliseconds. I need to update the text view continuously with these bulk data. What is recommended way to update Text view from a continuous background service?

Service Class

public class RecepService extends Service {

public static Handler mHandler;
StringBuilder hexstring;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    init();
}

private void init() {

    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {

                byte[] readBuf = (byte[]) msg.obj;
                int readBuflen = msg.arg1;

                // here will receive 1090 bytes of data
                // every 10 milliseconds
                Receivepatientattributes(readBuf,readBuflen);
            }
        }
    };
}

public void Receivepatientattributes(byte[] readBuf, int len) {

    String total_data = "";
    total_data = bytetohex(readBuf, len);
    MainActivity.recep.setText(MainActivity.recep.getText().toString() + "\t" +
            "" + total_data );
}

String bytetohex(byte[] txt, int len) {

    String p="";
    byte[] text = new byte[len];
    text = txt;

    hexstring = new StringBuilder();
    for (int j = 0; j < len; j++) {

        String hex= Integer.toHexString(0xFF & txt[j]);

        if (hex.length()==1) {
            hexstring.append("0");
        }

        hexstring.append(hex+" ");

    }

    p=p+hexstring.toString();
    return p;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
}
}
解决方案

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. :)

这篇关于不断更新的Andr​​oid的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:33