0

我正在使用这段代码:

  public void run() {
        handler.post(new Runnable() {
            public void run() {       
                try {
                    if (count==true)
                    {        
                        try 
                        {
                            r.stop();
                        } catch (Exception e) {}
                        tranca=false;
                        count=false; 

                        dlg.dismiss();
                        dlg.cancel();
                        dlg.dismiss();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
                             //task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
                        } else {
                            new AsyncCaller().execute();
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                }
            }
        });
    }
}, 15000);

问题是我不知道如何制作 new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

工作。问题是 asynctask 在蜂窝之后改变了它的工作方式,我找到了这段代码。它适用于 Honeycomb 以下的 Android 版本,但在我的 jellybean 中它不起作用。我可能正在使用new AsyncCaller().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 以错误的方式。异步任务是这样的:

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude1+","+longitude2+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


        JSONObject ret = getLocationInfo(); 
        JSONObject location2;
        String location_string;
        try {
            location2 = ret.getJSONArray("results").getJSONObject(0);
            location_string = location2.getString("formatted_address");
            Log.d("test", "formattted address:" + location_string);
            StringRua = (" " + location_string);
        } catch (JSONException e1) {
            e1.printStackTrace();

        }

         StringSMS = (" Latitude " + StringLat + " Longitude " + StringLon + " Ad " + StringRua);   




        EditText Search01; 
        String String01;

        Search01 = (EditText) findViewById(R.id.Search01);
        String01 = Search01.getText().toString();  


        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(String01, null, StringSMS , null, null);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
    }

    }   

我知道在stackoverflow中有这个完全相同的问题,但我尝试了很多解决方案,似乎没有人适合我。我只需要为 JellyBean 设备适当地调用 asynctask。请记住new AsyncCaller().execute(); 在蜂窝下方的设备中完美运行。谢谢你

编辑:仍然不工作,它不执行果冻豆设备中 dointhebackground 内部的内容(它显示加载,所以我知道它发送到 AsynctTask)。我已经尝试将在 Pre 和 Post 中需要做的事情放在doingthebackground之外,但由于“NetworkOnMainThreadException”问题,我不能这样做,这只是在新的android版本上强制关闭程序。

4

2 回答 2

0

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB),删除=这里。如果您的 BuildVersion 是 api 11,那么它已经可以串行运行了。

尝试将您的 targetSdk 更改为最新版本。AsyncTask 的处理方式(串行/并行)取决于 targetSdk。在此处阅读有关 AsyncTask 陷阱的更多信息

于 2013-09-08T20:01:07.780 回答
0

在此处复制异步任务代码https://stackoverflow.com/a/9509184/2190244,如果您想在不同的线程池执行器上执行异步任务,请通过调用 execute 将其用于 HC 前后。对于 HC 及更高版本的 AsyncTasks,使用的内部方法是串行执行器,除非您指定自己的执行器并将其传递给执行方法。

如果您不想使用该代码,您仍然可以为 pre 和 post HC 调用 execute,它应该可以正常工作

于 2013-09-08T19:18:49.750 回答