Dwidi
Dwidi

Reputation: 31

Start an activity inside a class that extends Thread

I am using a class from ACRA to handle exception when my application is force close and that class is extends Thread, name 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;
  }
}

}

I want to start an activity inside SendWorker class. I am using Handler and 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);

This is the UploadActivity.java that extends BaseDriveActivity.

public class UploadFileActivity extends BaseDriveActivity {
...
}

And this is the BaseDriveActivity.java

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

I run the project, everytime my application is force close, class SendWorker will be called but still not run the handler. Actually I want to upload a file to Google Drive that contains error bean from my application. Is that possible to start an activity inside the SendWorker class?

Upvotes: 1

Views: 397

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

get rid of Looper.prepare();, and change

Handler handler = new Handler();

to

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

or instantiate the handler on the UI Thread

Upvotes: 2

Related Questions