当我在 async loadview 上计算路线之间的距离时。结果是正常的。但完成计算后,进度对话框无法直接关闭。我看过Why so many GC_FOR_ALLOC in a simple app?。但是我仍然对这个结论感到困惑,因为我的结果已经完成了。
private class LoadViewTask extends AsyncTask<ArrayList<LatLng>, Integer, ArrayList<String>>
{
LatLng firs_point;
ArrayList<LatLng> objek_sync;
public LoadViewTask(LatLng firs_point, ArrayList<LatLng> objek_sync){
this.firs_point=firs_point;
this.objek_sync=objek_sync;
}
//Before running code in the separate thread
@Override
protected void onPreExecute()
{
//Create a new progress dialog
progressDialog = new ProgressDialog(MainActivity.this);
//Set the progress dialog to display a horizontal progress bar
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL );
//Set the dialog title to 'Loading...'
progressDialog.setTitle("Loading...");
//Set the dialog message to 'Loading application View, please wait...'
progressDialog.setMessage("Harap Tunggu!");
//This dialog can't be canceled by pressing the back key
progressDialog.setCancelable(false);
//This dialog isn't indeterminate
progressDialog.setIndeterminate(false);
//The maximum number of items is 100
progressDialog.setMax(100);
//Set the current progress to zero
progressDialog.setProgress(0);
//Display the progress dialog
progressDialog.show();
}
//The code to be executed in a background thread.
@Override
protected ArrayList<String> doInBackground(ArrayList<LatLng>... params)
{
/* This is just a code that delays the thread execution 4 times,
* during 850 milliseconds and updates the current progress. This
* is where the code that is going to be executed on a background
* thread must be placed.
*/
ArrayList<LatLng> objekarray= new ArrayList<LatLng>();
objekarray=objek_sync;
final Integer[] idx=new Integer[objekarray.size()] ;
final double[] data=new double[objekarray.size()] ;
try
{
//Get the current thread's token
synchronized (this)
{
//Initialize an integer (that will act as a counter) to zero
int counter = 0;
//While the counter is smaller than four
int proc=0;
for (int i=0; i< objekarray.size();i++)
{
counter++;
proc=(counter*100)/60;
/*DirectionJsonPharser _distance= new DirectionJsonPharser();
String url = getDirectionsUrl(firs_point, atmarray.get(i) );*/
//txtv.setText(_distance.distance_count(url));
idx[i]=i;
data[i]=(double) distance_AtoB(firs_point, objekarray.get(i));
/* data[i]= Double.parseDouble(_distance.distance_count(url));*/
publishProgress(proc);
this.wait(1);
}
Arrays.sort(idx, new Comparator<Integer>() {
@Override public int compare(final Integer o1, final Integer o2) {
return Double.compare(data[o1], data[o2]);
}
});
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
int cn=60;int cntr=0;int proc1=0;
ArrayList<String> ner=new ArrayList<String>();
ArrayList<String> qr=new ArrayList<String>();
try
{
//Get the current thread's token
synchronized (this)
{
for(int j=0;j<idx.length;j++){
cntr++;
proc1=cn + (cntr*100)/30;
globalvariable ak= new globalvariable();
//Toast.makeText(getApplicationContext(), idx[0], Toast.LENGTH_SHORT).show();
objek=ak.koneksi("master.php","status=" + idx[j] + "");
this.wait(10);
jArray= new JSONArray(objek);
JSONObject json_data=null;
json_data=jArray.getJSONObject(0);
if(j==0){
qr.add(json_data.getString("nm"));
ner.add(json_data.getString("kd"));
}else{
int ceksama=0;
for(int sem=0;sem<ner.size();sem++)
{
if(ner.get(sem).equals(json_data.getString("kd"))){
ceksama=1;
}
}
if(ceksama==0){
qr.add(json_data.getString("nm"));
ner.add(json_data.getString("kd"));
}
}
}
//objek=qr;
}
}
catch (InterruptedException e)
{
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(100);
return qr ;
//return null;
}
//Update the progress
@Override
protected void onProgressUpdate(Integer... values)
{
//set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}
//after executing the code in the thread
@Override
protected void onPostExecute(ArrayList<String> result)
{
//close the progress dialog
progressDialog.dismiss();
gps_mode=null;
Spinner spin_jalur= (Spinner) findViewById(R.id.spin_jalur);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin_jalur.setAdapter(adapter);
//initialize the View
}
}
这是我的原木猫......
08-30 09:19:05.660: D/dalvikvm(6631): GC_FOR_MALLOC freed 313K, 54% free 4673K/10055K, external 1304K/2106K, paused 31ms
08-30 09:19:06.050: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) NafHttpAuthStrategyDefault()
08-30 09:19:06.050: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) cached value : gbaSupportIsPossible=null
08-30 09:19:06.050: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) The current context is NOT a context of GBA service.
08-30 09:19:06.050: I/APACHE HTTP (thCr=28) - GbaSupportPermissionRequestCheckerImpl(6631): (thUse=28) isCurrentProcessRequestedGba()#finished result=false
08-30 09:19:06.060: I/APACHE HTTP (thCr=28) - GbaSupportPermissionRequestCheckerImpl(6631): (thUse=28) isCurrentProcessAllowedToUseGba()#started result=false
08-30 09:19:06.060: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) The GBA permission wasn't requested for this process.
08-30 09:19:06.060: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
08-30 09:19:06.060: I/APACHE HTTP (thCr=28) - NafRequestExecutorWrapperRedirectionHandler(6631): (thUse=28) It isn't GBA flow, redirection responses are not handled.
08-30 09:19:06.480: D/dalvikvm(6631): GC_FOR_MALLOC freed 312K, 54% free 4674K/10055K, external 1304K/2106K, paused 33ms
08-30 09:19:06.860: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) NafHttpAuthStrategyDefault()
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) cached value : gbaSupportIsPossible=null
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) The current context is NOT a context of GBA service.
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - GbaSupportPermissionRequestCheckerImpl(6631): (thUse=28) isCurrentProcessRequestedGba()#finished result=false
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - GbaSupportPermissionRequestCheckerImpl(6631): (thUse=28) isCurrentProcessAllowedToUseGba()#started result=false
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) The GBA permission wasn't requested for this process.
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - NafHttpAuthStrategyDefault(6631): (thUse=28) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
08-30 09:19:06.870: I/APACHE HTTP (thCr=28) - NafRequestExecutorWrapperRedirectionHandler(6631): (thUse=28) It isn't GBA flow, redirection responses are not handled.
08-30 09:19:07.300: D/dalvikvm(6631): GC_FOR_MALLOC freed 312K, 54% free 4674K/10055K, external 1304K/2106K, paused 37ms
08-30 09:19:07.640: W/System.err(6631): org.json.JSONException: Value 500 of type java.lang.Integer cannot be converted to JSONArray
08-30 09:19:07.650: W/System.err(6631): at org.json.JSON.typeMismatch(JSON.java:107)
08-30 09:19:07.650: W/System.err(6631): at org.json.JSONArray.<init>(JSONArray.java:91)
08-30 09:19:07.650: W/System.err(6631): at org.json.JSONArray.<init>(JSONArray.java:103)
08-30 09:19:07.650: W/System.err(6631): at com.example.ankara.MainActivity$LoadViewTask.doInBackground(MainActivity.java:241)
08-30 09:19:07.650: W/System.err(6631): at com.example.ankara.MainActivity$LoadViewTask.doInBackground(MainActivity.java:1)
08-30 09:19:07.650: W/System.err(6631): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-30 09:19:07.650: W/System.err(6631): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-30 09:19:07.650: W/System.err(6631): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-30 09:19:07.650: W/System.err(6631): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-30 09:19:07.650: W/System.err(6631): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-30 09:19:07.650: W/System.err(6631): at java.lang.Thread.run(Thread.java:1019)
这是我的koneksi方法
public class globalvariable {
String result="";
public String koneksi(String table,String val_idat){
InputStream isr= null;
String hasil="";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://angkot.esy.es/" + table + "?" + val_idat );
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}
catch(Exception ex){
}
try
{
BufferedReader reader= new BufferedReader(new InputStreamReader(isr, "iso-8859-1"),8);
StringBuilder sb= new StringBuilder();
String line=null;
while((line=reader.readLine()) != null){
sb.append(line+"\n");
}
isr.close();
hasil=sb.toString();
}catch(Exception ex){
hasil=ex.toString();
}
return hasil;
}
}