2 qq 35454005 qq_35454005 于 2016.09.13 22:53 提问

初学android开发 ,请问如何在主子线程间传递消息,请看详情

我想在启动主activity后立即向thread发送消息并更新一个Textview,可是它并没有更新,而是在退出程序后(不是彻底结束)再返回activity是时才更新 为什么 一下是程序(有无关变量,请无视)
MainActivity
package com.example.administrator.a77777;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
static Handler handler1=new Handler();
static Handler handler2=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview=(TextView)findViewById(R.id.te);
Button btnObj = (Button) findViewById(R.id.bu);
final LooperThread looperThread = new LooperThread(this);
looperThread.start();
looperThread.handlert1.sendEmptyMessage(1);
handler2=new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
textview.setText(msg.obj.toString());
}
};
btnObj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

            looperThread.handler.sendEmptyMessage(1);

        }
    });
    handler1=new Handler(){
        @Override
        public void handleMessage(Message msg){
            textview.setText(msg.obj.toString());
        }
    };
}

}

Looperthread
package com.example.administrator.a77777;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import com.example.administrator.a77777.MainActivity;
/**

  • Created by Administrator on 2016/9/13.
    /
    class LooperThread extends Thread {
    public Handler handler=new Handler();
    static public Handler handlert1=new Handler();
    public static Context context;
    public LooperThread(Context con){
    context=con;
    }
    @Override
    public void run() {
    super.run();
    Looper.prepare();
    Text helper=new Text(context);
    final SQLiteDatabase db=helper.getWritableDatabase();
    /
    启动时更新数据*/
    QiDongGengXin();
    handlert1 = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    Message m=Message.obtain();
    m.obj="这是在启动时从子线程传来的消息";
    MainActivity.handler2.sendMessage(m);
    }
    };
    handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    Message m=Message.obtain();
    m.obj="这不是在启动时从子线程传来的消息";
    MainActivity.handler1.sendMessage(m);
    }
    };

    Looper.loop();//loop()会调用到handler的handleMessage(Message msg)方法,所以,写在下面;
    

    }
    public void QiDongGengXin(){
    String Szsy;
    SharedPreferences sp =context.getSharedPreferences("mrsoft",0);
    Szsy=sp.getString("restmoney","DULL");
    String S1= sp.getString("S1", "dull");
    String S2= sp.getString("S2", "dull");
    String S3= sp.getString("S3", "dull");
    String S4= sp.getString("S4", "dull");
    }
    }

2个回答

devmiao
devmiao   Ds   Rxr 2016.09.13 23:58
qq_35454005
qq_35454005 你看我的详细好不
大约一年之前 回复
CodeFarmerCXY
CodeFarmerCXY   2017.03.25 16:05

可以使用bandle传送信息

Csdn user default icon
上传中...
上传图片
插入图片