0
private class MyTask extends AsyncTask<String, Integer, Integer> {
    int number;
    private String content = null;
    private boolean error = false;
    Context mContext;
    int NOTIFICATION_ID = 1;
    Notification mNotification;
    NotificationManager mNotificationManager;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Boolean doInBackground(String... args) {

        return true;
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (response) {

        //Build the notification using Notification.Builder
        Notification.Builder builder = new Notification.Builder(mContext)
                .setSmallIcon(R.mipmap.app_icon)
                .setAutoCancel(true)
                .setContentTitle(contentTitle)
                .setContentText(contentText);

        Intent intent = new Intent(mContext, CaptureActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
                intent, 0);
        builder.setContentIntent(pendingIntent);

        //Get current notification
        mNotification = builder.getNotification();

        //Show the notification
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }
}

The above codes are the part of my asynctask. As the process of the doinbackground would be a bit long, I hope to let user to intent or move to another pages so that they don't have to wait too long and stick with that activity. Is there a way to move to another activity when the doinbackground start? And also if it can pop up the activity when the doinbackgound is finished, it would be great for me. Thank you very much.

ng2b30
  • 351
  • 6
  • 18
  • you cannot start an activity at doInBackground and offcourse you are left with onPostExecute now. There you can do any UI task. – Ankush Bist Jan 17 '17 at 11:49
  • In onPostExecute, task has been finished the doInBackground. This is not suitable for me. – ng2b30 Jan 17 '17 at 12:06

1 Answers1

2

In this situation I suggest to use Service or IntentService instead of AsyncTask - if doInBackground is long, user can go to other activities and this may cause memory leak

Tomasz Czura
  • 2,414
  • 1
  • 14
  • 18
  • If I want to pause the app but still running the asynctask like downloading app in google play, are there any suitable way to do that? Thanks. – ng2b30 Jan 17 '17 at 12:04
  • 1
    This is suitable way - using `Service` – Tomasz Czura Jan 17 '17 at 12:06
  • Thank you. Should I use service with asynctask? Because I want to do tasks in parallel. – ng2b30 Jan 17 '17 at 12:08
  • 1
    if you are in need to move to next activity without getting response from the async task then you dnt need to call async task. You can call async task using service. – Ankush Bist Jan 17 '17 at 12:13
  • You do not need to use `AsyncTask`. If you want to make some background long running task, you can use `IntentService` - it will be run on other thread. You can also use a normal `Service`, but then you have to use some Java `Thread` – Tomasz Czura Jan 17 '17 at 12:28
  • Would you mind to do a simple example for me? I cannot find an example that can show me how to leave the activity when IntentService is running.Thank you. – ng2b30 Jan 17 '17 at 12:55
  • What exactly you want in your activity? You just start IntentService like here: https://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183, and you can do whatever you want in your activity – Tomasz Czura Jan 17 '17 at 12:57
  • I am only would quiz the current activity and move to another activity. The activity is simple contain some buttons and textviews. – ng2b30 Jan 17 '17 at 13:42