当前位置: 代码迷 >> Android >> UI线程又被堵塞了吗?确实另开了线程
  详细解决方案

UI线程又被堵塞了吗?确实另开了线程

热度:46   发布时间:2016-05-01 17:47:01.0
UI线程又被阻塞了吗?确实另开了线程
运行时发现,那个不确定的小圆形进度条死在那里不动,不打转,到底哪里出错了???
代码如下:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import com.dyb.partoneplay.R;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class Reader extends Activity {
private String text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示第一屏,不需要标题栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_loading);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
new readThread(loadTask, loading).start();
super.onResume();
}


private Handler loadTask = new Handler();

private Runnable loading = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub

//读取文件内容
try {Thread.sleep(10000);
InputStream Fin = getResources().openRawResource(R.raw.pal4);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = Fin.read(buffer)) != -1){
oStream.write(buffer, 0, len);
}
text = oStream.toString("GBK");
Fin.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//退出全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//修改布局文件
Reader.this.setContentView(R.layout.activity_read);
((TextView)findViewById(R.id.booktv)).setText(text);
}
};

}

class readThread extends Thread{
private Handler mHandler;
private Runnable mRunnable;

public readThread(Handler handler, Runnable runnable){
this.mHandler = handler;
this.mRunnable = runnable;
}

@Override
public void run() {
// TODO Auto-generated method stub
mHandler.postDelayed(mRunnable, 200);
}

}


布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/layout" >
   
  <ImageView
  android:id="@+id/bgloading"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:contentDescription="@string/loading"
  android:src="@drawable/cover" />
   

  <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:layout_marginBottom="20dp"
  android:layout_marginRight="20dp"
  android:text="@string/wenxin"
  android:textSize="15sp"
  相关解决方案