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);
}
}
上面的代碼是我的asynctask的一部分。由於doinbackground的過程會有點長,我希望讓用戶意圖或移動到另一個頁面,以便他們不必等待太久並堅持該活動。有沒有辦法將移動到另一個活動當doinbackground開始?而且,如果它可以當doinbackgound完成時彈出活動,這對我來說會很好。非常感謝你。做AsyncTask時轉移到其他活動
你不能在doInBackground開始活動,並offcourse你是左現在用onPostExecute。在那裏你可以做任何UI任務。 –
在onPostExecute中,任務已完成doInBackground。這不適合我。 – ng2b30