Android停止启动AsyncTask

时间:2013-10-11 18:40:32

标签: android android-asynctask

我有一个AsyncTask(AddressUpdaterTask),它从AutoCompleteTextView获取文本,解析它,然后显示地址下拉列表,就像在谷歌地图中键入地址一样。每次用户键入一个字符时,都会执行一个新的AddressUpdaterTask。我的问题是这没有效率,因为当用户开始输入时,如果他输入的速度足够快,则执行新的AsyncTask而前一个可能尚未完成执行。

所以我这样做了:

AddressUpdaterTask addressUpdaterTask = new AddressUpdaterTask();

如果addressUpdaterTask是全局的,当用户键入一个字符时,我会这样做:

addressUpdaterTask.cancel(true);
addressUpdaterTask.execute();

所以我可以操作AddressUpdaterTask的一个实例。但这种方式不起作用:( 我怎样才能实现我想要的东西?

编辑:这是我的代码:

public class RegisterDoctor extends Activity {

        .
        .
        .

    private AutoCompleteTextView addressField;
    private ArrayAdapter<String> adapter;
    private Filter filter;
    private List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();
    private SimpleAddress currentAddress = null;
    AddressUpdaterTask addressUpdaterTask = new AddressUpdaterTask();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_doctor);

        .
        .
        .


        //AutoCompleteTextView Creation
        addressField = (AutoCompleteTextView) findViewById(R.id.addressEditText);
        addressField.setThreshold(ADDRESS_TRESHOLD);
        addressField.setHint("Οδός Αριθμός, Περιοχή");
        filter = new Filter() {
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                adapter.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Log.i("FILTER_ACTION", "Filter:" + constraint);
                if (constraint != null && constraint.length() > ADDRESS_TRESHOLD) {
                    Log.i("FILTER_ACTION", "doing a search ..");
                    addressUpdaterTask.cancel(true);
                    addressUpdaterTask.execute();
                }
                return null;
            }
        };
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line) {
            public android.widget.Filter getFilter() {
                return filter;
            }
        };
        addressField.setAdapter(adapter);
        adapter.setNotifyOnChange(false);
        addressField.setOnItemClickListener(itemClicked);

        .
        .
        .

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


    private AdapterView.OnItemClickListener itemClicked = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
            currentAddress = simpleAddresses.get(pos);
            Log.i("ITEM_CLICKED", simpleAddresses.get(pos).getFormatted_address());
        }
    };

        .
        .
        .

    public class AddressUpdaterTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //adapter.clear();
        }


        @Override
        protected Void doInBackground(Void... voids) {
            try {
                simpleAddresses = new JsonHelper().getAddresses(addressField.getText().toString());
            } catch (NullPointerException e) {
            }
            return null;
        }
@Override
        protected void onPostExecute(Void aVoid) {
            int size = simpleAddresses.size();

            if (size > 0) {
                adapter.clear();
                for (int i = 0; i < size; i++) {
                    adapter.add(simpleAddresses.get(i).getFormatted_address());
                    Log.i("ADDRESS_ADDED", simpleAddresses.get(i).getFormatted_address());
                }
                adapter.notifyDataSetChanged();
                addressField.showDropDown();
            }
            super.onPostExecute(aVoid);
        }
    }
        .
        .
        .
}

编辑2 :以下是用户输入=&gt;时的logcat 6个字符(我已经放入的阈值)和AsyncTask可以执行:

10-11 18:56:59.795    1465-1494/com.user.project I/FILTER_ACTION: Filter:panepi
10-11 18:57:02.695    1465-1494/com.user.project I/FILTER_ACTION: Filter:panepis
10-11 18:57:02.695    1465-1494/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:06.544    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepist
10-11 18:57:06.544    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:06.555    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:09.004    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepisti
10-11 18:57:09.004    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:09.014    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:09.904    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepistim
10-11 18:57:09.904    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:09.914    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:10.194    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepistimi
10-11 18:57:10.194    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:10.194    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:11.865    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepistimio
10-11 18:57:11.865    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:11.954    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:12.196    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepistimiou
10-11 18:57:12.196    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:12.295    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
10-11 18:57:14.795      404-966/com.android.inputmethod.latin E/ActivityThread: Failed to find provider info for com.android.inputmethod.latin.dictionarypack
10-11 18:57:14.805      404-966/com.android.inputmethod.latin E/BinaryDictionaryGetter: Could not find a dictionary pack
10-11 18:57:14.815    1465-1496/com.user.project I/FILTER_ACTION: Filter:panepistimiou
10-11 18:57:14.815    1465-1496/com.user.project I/FILTER_ACTION: doing a search ..
10-11 18:57:14.815    1465-1496/com.user.project W/Filter: An exception occured during performFiltering()!
        java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.user.project.RegisterDoctor$2.performFiltering(RegisterDoctor.java:104)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)

1 个答案:

答案 0 :(得分:0)

实现自动完成时,通常会引入一些延迟,以便多次调用服务而不会返回结果。

尝试添加一个变量(可能是静态的),用于跟踪用户何时输入其第一个输入,并且仅在经过一段时间(500-750ms相当常见)后执行搜索。这是一个更好的用户体验,但不能解决您的初始问题。

对于“双重执行”问题,只需在请求开始时设置一个设置为true的条件,并在完成时设置为false,并且在设置变量时不允许另一个请求启动。

这样的事情:

bool requestInProgress = false;

...

@Override
protected void onPreExecute() {
    super.onPreExecute();
    requestInProgress = true;
}

protected void onPostExecute(Void aVoid) {
    int size = simpleAddresses.size();

    if (size > 0) {
        adapter.clear();
        for (int i = 0; i < size; i++) {
            adapter.add(simpleAddresses.get(i).getFormatted_address());
            Log.i("ADDRESS_ADDED", simpleAddresses.get(i).getFormatted_address());
        }
        adapter.notifyDataSetChanged();
        addressField.showDropDown();
    }
    super.onPostExecute(aVoid);
    requestInProgress = false;
}

以上......

@Override
protected FilterResults performFiltering(CharSequence constraint) {
    Log.i("FILTER_ACTION", "Filter:" + constraint);
    if (constraint != null && constraint.length() > ADDRESS_TRESHOLD 
           && !requestInProgress) {
        Log.i("FILTER_ACTION", "doing a search ..");
        addressUpdaterTask.cancel(true);
        addressUpdaterTask.execute();
    }
    return null;
}