2012-07-31 78 views
-1

我已經實現代碼的形式下面鏈接查看應用程序 How to intent to another page on android/pop up a message from idle time?如何在android中顯示asyntask的nonUI活動對話框?

的空閒時間,而不是使用線程我用asyntask ......現在我的問題,一旦達到空閒time..i要顯示對話框用戶應用程序需要重新登錄結束從登錄活動.. 我怎樣才能調用對話從的AsyncTask onpostExcute

public class session extends AsyncTask<Void,Void,Void> { 
private static final String TAG=session.class.getName(); 
private long lastUsed; 
private long period; 
private boolean stop; 
Context context; 


final Dialog dialog = new Dialog(context); 
@Override 
protected Void doInBackground(Void... params) { 
    // TODO Auto-generated method stub 
    //here i do the process....... 
} 
@Override 
protected void onPostExecute(Void x){ 
     //stuff to be done after task executes(done on UI thread) 

    // For Dialog Button********************************** 
    dialog.setContentView(R.layout.dialog); 

    dialog.setTitle("Result"); 

    final TextView dialogtxt = (TextView) dialog 
      .findViewById(R.id.textView1); 

    final Button closeButton = (Button) dialog 
      .findViewById(R.id.button1); 

    closeButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      dialog.dismiss(); 
     } 
    }); 

    dialogtxt.setText("session time out"); 
    dialog.show(); 

    // **************************************************** 

} 
@Override 
protected void onPreExecute(){ 
     //stuff to be done after task executes(done on UI thread) 

} 

} 
+0

你的問題不清楚。放一些示例代碼 – waqaslam 2012-07-31 09:36:28

+1

您可以在'onPostExecude()'中與UI線程進行交互。我沒有看到你的問題。 – nkr 2012-07-31 09:37:19

+0

我更新code.please回覆如何顯示對話框 – vijay 2012-07-31 10:31:16

回答

-1

的的AsyncTask需要得到上下文。 如果您的Asynctask嵌入到活動中,只需將java Activity.this作爲上下文來調用。 你也可以把一個上下文作爲一個字段放在Asynctask中,然後把它作爲一個arg賦給Asynctask。

你可以調用onPostExecute中的Dialog.show,它在UI Thread上。

該樣品的AsyncTask被內嵌到活動

公共類AsyncDialogBu​​ilder擴展的AsyncTask {

private Context context = DriverOnTripActivity.this; 
    private final AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    private Integer remoteAllWaitinOnCount; 

    public Context getContext() { 
     return context; 
    } 

    public void setContext(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected Integer doInBackground(Integer... integers) { 
     remoteAllWaitinOnCount = User.getRemoteAllWaitinOnCount(latestClosestKojo.getRemoteId()); 
     if (remoteAllWaitinOnCount > 0) { 
      try { 
       makeDialog(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return 100; 
     } else { 
      return 99; 
     } 
    } 

    private void makeDialog() { 
     dialog.setTitle(latestClosestKojo.getName() 
       + " - " 
       + remoteAllWaitinOnCount 
       + " Kojoalas"); 
     dialog.setPositiveButton("S'arreter", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       isDialogPrompted = false; 
       dialogInterface.dismiss(); 
       goToOnBoardingActivity(); 
      } 
     }); 
     dialog.setNegativeButton("Ignorer", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       isDialogPrompted = false; 
       dialogInterface.dismiss(); 
      } 
     }); 
    } 

    @Override 
    protected void onPostExecute(Integer integers) { 
     if (integers >= 100 && dialog != null) { 
      dialog.show(); 
      isDialogPrompted = true; 
     } 
    } 
} 
+0

HI..has單獨的類與asyntask擴展..我可以如何調用上下文 – vijay 2012-07-31 10:23:02

+0

嘗試將其設置在您的活動。 MyAsynTask m = new MyAsynTask(); m.setContext(this); – Dam 2012-07-31 11:47:05

0

你可以通過調用該對話框做到這一點從除doInBackground方法的方法中的任何一個。

您可以在onPreExecute中調用它,並在那裏顯示對話框,在完成後臺任務後,您可以使用onPostExecite方法取消它。如果你想更多的控制,你也可以使用onProgressUpdate來完成。只需調用publishProgress調用後臺任務的進度,並覆蓋onProgressUpdate方法並在其中執行任何操作。

這是一個從docs中選取的例子。

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 
相關問題