android 子线程中不能够更新ui已经根深蒂固在我的脑海里,当时也就理所当然的觉得子线程中也是不能够进行界面的跳转的,可是在后来的学习中,发现居然是能够通过子线程来进行activity的跳转时,立刻把我给震惊了.赶紧滚去写代码測试了下.结果真的能够.顿时泪奔.特此记录一下.与君共勉

activity:

package com.example.testchildthreadgootheractivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
//第一个button,通过主线程跳转activity,并传递内容
findViewById(R.id.btn_go1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("test", "main thread:" + Thread.currentThread().getName()
+ "id:" + Thread.currentThread().getId());
Intent intent = new Intent(context, OtherActivity.class);
// 測试传递字符串
intent.putExtra("hello", "hello im main thread");
startActivity(intent);
}
}); //第二个button,通过子线程跳转activity,并传递内容
findViewById(R.id.btn_go2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
Log.e("test", "child thread:"
+ Thread.currentThread().getName() + "id:"
+ Thread.currentThread().getId());
Intent intent = new Intent(context, OtherActivity.class);
// 測试传递字符串
intent.putExtra("hello", "hello im child thread");
startActivity(intent);
}
}).start(); }
});
}
}

測试得出结论.子线程能够进行activity的跳转.以及通过intent传递内容

05-11 15:53