我正在使用这段代码:
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版本上强制关闭程序。