如何等待活动在asynctask中完成

时间:2016-02-23 14:17:47

标签: android android-asynctask popup async-await

我知道AsyncTask的目的是与应用程序的其他任务异步运行并在后台完成,但显然我需要这样做,我需要从AsyncTask启动一个活动,因为我无法扩展活动在这堂课中我不能使用startactivityforresult,所以我怎么能等到我的活动结束?

这是我的代码:

public class PopUp  extends Activity  {

private static final String TAG = "PopUp";
ArrayList<String> value = null;

ArrayList<String> usernames;

@Override
protected void onCreate(Bundle savedInstanceState) {

    final Global globalVariable = (Global) getApplicationContext();
    globalVariable.setUsername("");
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        value = extras.getStringArrayList("EXTRA_SESSION_ID");
    }

    usernames = value;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupactivity);

    final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            Button btnSelect = (Button) popupView.findViewById(R.id.select);
            Spinner popupSpinner = (Spinner) popupView.findViewById(R.id.popupspinner);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(PopUp.this, android.R.layout.simple_spinner_item, usernames);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            popupSpinner.setAdapter(adapter);
            popupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    globalVariable.setUsername(usernames.get(arg2));



                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });
            btnSelect.setOnClickListener(new Button.OnClickListener() {

                                             @Override
                                             public void onClick(View v) {
                                                 globalVariable.setPopupdone(true);
                                                 popupWindow.dismiss();
                                                 finish();

                                             }
                                         }
            );

            popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.poupup_menu, menu);
    return true;
}}

我的活动课程:

{{1}}

0 个答案:

没有答案