1

独自のクラスでAsyncTaskを作成しました。クラスBから呼び出して、現在実行中のクラスAの何かを表示したいと思いますActivity。使用する必要がありますrunOnUiThread()か?または私はどういうわけ setOwnerActivity(Activity)か?何が悪いのかわからない。

これがタスクです:

public class popTask extends AsyncTask<Void, Void, String> {
    private Context con;

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        final Dialog dialog = new Dialog(con);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("New & Hot advertise");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.yoda);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }


    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }

    public popTask(Context con){
        this.con=con;
    }
}
4

1 に答える 1

0

アクティビティAへの参照を持つこの非同期タスクのコンストラクターを作成できます。

例えば:

public popTask(ClassA ref) {
    super();
    this.ref = ref;
    // do stuff
}

次に、このアクティビティへの参照を使用して、アクティビティのメソッドを呼び出すことができます。何かを表示したい場合、この呼び出しは常にonProgressUpdateまたはonPostExecuteで実行する必要があります(doInBackgroundでは実行しないでください)。

それが役に立てば幸い!

于 2012-06-12T19:25:21.470 に答える