-1

アプリケーションのアイドル時間を確認するために、以下のリンクからコードを実装し ました。Androidの別のページにインテントする方法/アイドル時間からメッセージをポップアップする方法は?

代わりにスレッドを使用してasyntaskを使用しました...アイドル時間に達したら問題が発生します。ユーザーアプリケーションにダイアログを表示したいのは、ログインアクティビティからの再ログインを終了することです。非同期タスク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)

}

}
4

2 に答える 2

0

これを行うには、doInBackgroundメソッド以外のいずれかのメソッドからダイアログを呼び出します。

onPreExecuteで呼び出してダイアログを表示し、バックグラウンドタスクが完了したら、onPostExeciteメソッドからキャンセルできます。さらに詳細な制御が必要な場合は、onProgressUpdateを使用して行うこともできます。publishProgressを呼び出してバックグラウンドタスクから進行状況をディスパッチし、onProgressUpdateメソッドを上書きして、そこでやりたいことを実行します。

これは、ドキュメントから抜粋した例です。

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");
     }
 } 
于 2012-07-31T09:45:57.370 に答える
-1

Asynctask はコンテキストを取得する必要があります。Asynctask がアクティビティに埋め込まれている場合は、Java Activity.this をコンテキストとして呼び出すだけです。コンテキストをフィールドとして Asynctask に配置し、それを引数として Asynctask に渡すこともできます。

onPostExecute で Dialog.show を呼び出すことができます。これは UI スレッドにあります。

このサンプル AsyncTask はアクティビティに埋め込まれています

public class AsyncDialogBu​​ilder extends 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;
        }
    }
}
于 2012-07-31T09:37:49.723 に答える