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

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,但仍不运行处理程序。实际上,我想将文件上传到Google云端硬盘,其中包含来自我的应用程序的错误Bean。
是否可以在SendWorker类中启动活动?

最佳答案

摆脱Looper.prepare();,改变

Handler handler = new Handler();




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


或在handler上实例化UI Thread

10-06 10:39