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); 
    } 
} 

上面的代碼是我的asynctask的一部分。由於doinbackground的過程會有點長,我希望讓用戶意圖或移動到另一個頁面,以便他們不必等待太久並堅持該活動。有沒有辦法將移動到另一個活動當doinbackground開始?而且,如果它可以當doinbackgound完成時彈出活動,這對我來說會很好。非常感謝你。做AsyncTask時轉移到其他活動

+0

你不能在doInBackground開始活動,並offcourse你是左現在用onPostExecute。在那裏你可以做任何UI任務。 –

+0

在onPostExecute中,任務已完成doInBackground。這不適合我。 – ng2b30

回答

2

在這種情況下,我建議使用ServiceIntentService,而不是AsyncTask - 如果doInBackground長,用戶可以去其他活動,這可能會導致內存泄露

+0

如果我想暫停應用程序,但仍然運行像在谷歌播放下載應用asynctask,是否有任何合適的方式來做到這一點?謝謝。 – ng2b30

+1

這是合適的方式 - 使用'服務' –

+0

謝謝。我應該使用asynctask服務嗎?因爲我想同時做任務。 – ng2b30