Android - 线程没有执行

时间:2013-03-20 23:25:34

标签: java android multithreading

所以是的,我的线程没有执行,或者在其中执行代码。我只是想从我的SD卡中运行一个shell脚本,并显示一个“加载”圆圈或“进度”圆圈或任何你想要调用的东西。当我单击按钮运行线程时,我得到进度/加载条/圆,但它只是坐在那里,什么都不做。我看了一些例子,但仍然无法弄清楚我做错了什么。这是我的代码:

package com.cydeon.plasmamodz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

import com.stericson.RootTools.*;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Install extends Activity{

private static ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.install);
    Button bInstall = (Button) findViewById(R.id.bInstallTheme);
    bInstall.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showDialog();
        }
    });
}


public void showDialog(){
    progressDialog = ProgressDialog.show(Install.this, "", "Installing Theme", true);
    Thread thread = new Thread();
    thread.start();
}

public void run(){
    try{
        Thread.sleep(1000);
        CommandCapture command = new CommandCapture(0, "su", "sh /sdcard/plasma/scripts/install.sh");
        try {
            RootTools.getShell(true).add(command).waitForFinish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (RootDeniedException e) {
            e.printStackTrace();
        }
    }catch (InterruptedException e){
        e.printStackTrace();
    }
    handler.sendEmptyMessage(0);
}

private static Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        progressDialog.dismiss();
    }
};

}

那么,我做错了吗?为什么不运行我的代码?谢谢!

1 个答案:

答案 0 :(得分:1)

线程不知道要运行什么。变化

public class Install extends Activity{

public class Install extends Activity implements Runnable {

并更改

Thread thread = new Thread();

Thread thread = new Thread(this);