我在我的活动中创建了一个新线程。在它里面运行(..)我写了一个代码来从谷歌获取地理代码。当我收到 JSON 响应并在处理后将其发送给处理程序。在处理程序内部,我发现新线程已死亡。
这是我的运行()
@Override
public void run() {
try {
Thread.sleep(4000);
Log.d("Thread", "---Is Running---");
getCityName = getIntent().getStringExtra("city");
Log.d("----getCityName----", getCityName);
HttpRetriever httpObj = new HttpRetriever();
String geocodeUrl = "http://maps.googleapis.com/maps/api/geocode/json?address="
+ getCityName + "&sensor=true";
String url = geocodeUrl.replace(" ", "%20");
Log.d("----geocodeUrl----", url);
geocodeResponse = httpObj.retrieve(url);
Log.d("----Json Response----", geocodeResponse);
JSONObject jsonObject = new JSONObject(geocodeResponse);
JSONArray jsonArray = (JSONArray) jsonObject.get("results");
if (jsonArray.length() > 0) {
jsonObject = (JSONObject) jsonArray.get(0);
jsonObject = (JSONObject) jsonObject.get("geometry");
JSONObject location = (JSONObject) jsonObject
.get("location");
Double lat = (Double) location.get("lat");
Double lng = (Double) location.get("lng");
mypoint = new GeoPoint((int) (lat * 1000000),
(int) (lng * 1000000));
Log.d("-----Lat LOng-----", "" + lat + " " + lng);
addOverlayOnMap(mypoint, getCityName);
myMap.getController().animateTo(mypoint);
myMap.getController().setZoom(6);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
这是我的处理程序。
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
pdDialog.dismiss();
if (geocodeResponse != null) {
if (t.isAlive() == true) {
Log.v("------Thread-----",
"---Inside Handler----Thread is Alive!!!!----");
} else {
Log.v("------Thread-----",
"---Inside Handler---Thread is Died!!!!----");
}
Toast.makeText(getApplicationContext(), " Ya got the json",
Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Not Success",
Toast.LENGTH_LONG).show();
}
};
我只是想知道为什么我的线程在处理程序中停止了?虽然我不会在任何地方停止它。我在模拟器中测试代码
如果你有问题,可以分享。
提前致谢