执行 AsyncTask 时移动到其他 activity
Move to other activity when doing AsyncTask
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);
}
}
以上代码是我的异步任务的一部分。由于doinbackground的过程会有点长,我希望让用户去意图或移动到另一个页面,这样他们就不会必须等待太久并坚持 activity。 doinbackground start时,有没有办法移动到另一个activity?而且,如果它可以在 doinbackgound 完成时弹出activity,那对我来说会很棒。非常感谢你。
在这种情况下,我建议使用 Service
或 IntentService
而不是 AsyncTask
- 如果 doInBackground
很长,用户可以去其他活动,这可能会导致内存泄漏
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);
}
}
以上代码是我的异步任务的一部分。由于doinbackground的过程会有点长,我希望让用户去意图或移动到另一个页面,这样他们就不会必须等待太久并坚持 activity。 doinbackground start时,有没有办法移动到另一个activity?而且,如果它可以在 doinbackgound 完成时弹出activity,那对我来说会很棒。非常感谢你。
在这种情况下,我建议使用 Service
或 IntentService
而不是 AsyncTask
- 如果 doInBackground
很长,用户可以去其他活动,这可能会导致内存泄漏