在扩展Thread

时间:2015-05-28 07:56:21

标签: java android multithreading runnable android-handler

当我的应用程序强制关闭并且该类扩展为Thread,名称为SendWorker.java时,我正在使用ACRA中的一个类来处理异常。

SendWorker.java

final class SendWorker extends Thread {

  private final Context context;
  private final boolean sendOnlySilentReports;
  private final boolean approvePendingReports;
  private final CrashReportFileNameParser fileNameParser = new CrashReportFileNameParser();
  private final List<ReportSender> reportSenders;

  public SendWorker(Context context, List<ReportSender> reportSenders, boolean sendOnlySilentReports, boolean approvePendingReports) {
    this.context = context;
    this.reportSenders = reportSenders;
    this.sendOnlySilentReports = sendOnlySilentReports;
    this.approvePendingReports = approvePendingReports;
  }
}

}

我想在SendWorker类中启动一个活动。我正在使用Handler和Runnable。

Looper.prepare();

Handler handler = new Handler();

Runnable run = new Runnable() {
  @Override
  public void run() {
    Intent in = new Intent(context, UploadFileActivity.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);
  }
};
handler.post(run);

这是扩展BaseDriveActivity的UploadActivity.java。

public class UploadFileActivity extends BaseDriveActivity {
...
}

这是BaseDriveActivity.java

public abstract class BaseDriveActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
...
}

我运行项目,每次我的应用程序强制关闭时,将调用类SendWorker但仍然不运行处理程序。实际上我想将一个文件上传到包含我的应用程序错误bean的Google Drive。 可以在SendWorker类中启动一个活动吗?

1 个答案:

答案 0 :(得分:2)

摆脱Looper.prepare();,然后更改

Handler handler = new Handler();

Handler handler = new Handler(Looper.getMainLooper());

或实例化handler

上的UI Thread