【发布时间】:2015-10-06 02:38:16
【问题描述】:
我正在使用此代码:
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);
问题是我不知道怎么做 新的 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 中需要做的事情放在 dothebackground 之外,但由于“NetworkOnMainThreadException”问题,我不能这样做,这只是在新的 android 版本上强制关闭程序。
【问题讨论】:
-
你的 minSdk 和 targetSdk 是什么?
-
android:minSdkVersion="8" android:targetSdkVersion="11"
-
在下面查看我的答案
-
所以不要试图拖钓,而是定义“不工作”。您正在从不是 UI 线程的线程上的屏幕组件中读取数据。所以我猜你正在阅读的数字是空的,并且 doInBackground 是没有操作的。为什么不让 AsyncCaller 成为 AsyncCaller
并实际定义广播的pendingIntent。这样你就可以在 UI 线程上执行 executeOnExecutor(executor, String.valueOf(textView.getText()); 并且成功将被广播。
标签: android android-asynctask versions