Jump to content

Android Threads


Scar19
 Share

Recommended Posts

Sveiki! Android app veidoshanai pieversos tikai nesen, meeginu uztaisiit app, kas ieguust GPS kordinaatas un ieliek taas datubaazee.

Esosais kods straadaa bet tikai uz pirmo onLocationChanged, izsaucot to veelreiz tiek izmests errors un programma apstaajas, man aizdomas ira ka vaina ira ar Thread, jo errors saka ka threds jau ira palaists, meeginaaju lietot

checkUpdate.Interrupt();

pirms saaku to thread no saakuma bet tas neliidzeeja.Meeginaaju neizmantot Thread pie konekcijas ar serveri bet tad konekcija vispaar nenotiek.

package asd.dfghg;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
   public class SanchaActivity extends Activity{
    private String html = "";
    private Handler mHandler;
    private String latitude;
    private String longitude;
    @Override
    public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    //Button button = (Button)findViewById(R.id.button1);
	   // button.setOnClickListener(this);
	    mHandler = new Handler();
	    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	    LocationListener mlocListener = new MyLocationListener();
	    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
	  //public void onClick(View v) {
	 //checkUpdate.start();
    //}
    }
	 ///////////////////////////////////////////////////////////////////
	 public class MyLocationListener implements LocationListener {
	  public void onLocationChanged(Location loc) {
	   loc.getLatitude();
	   loc.getLongitude();
	   latitude = Double.toString(loc.getLatitude());
	   longitude = Double.toString(loc.getLongitude());
	   checkUpdate.start();
	  }
	  public void onProviderDisabled(String arg0) {
	   // TODO Auto-generated method stub
	  }
	  public void onProviderEnabled(String arg0) {
	   // TODO Auto-generated method stub
	  }
	  public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	   // TODO Auto-generated method stub
	  }
    public Thread checkUpdate = new Thread() {
	    public void run() {
		    try {
			    URL updateURL = new URL("http://links/mans.php?latitude=" + latitude + "&longitude=" + longitude);
			    URLConnection conn = updateURL.openConnection();
			    InputStream is = conn.getInputStream();
			    BufferedInputStream bis = new BufferedInputStream(is);
			    ByteArrayBuffer baf = new ByteArrayBuffer(50);
			    int current = 0;
			    while((current = bis.read()) != -1){
				    baf.append((byte)current);
			    }
			    html = new String(baf.toByteArray());
			   mHandler.post(showUpdate);
		    } catch (Exception e) {
		    }
	    }
    };
    private Runnable showUpdate = new Runnable(){
	    public void run(){
		 TextView myTextView = (TextView) findViewById(R.id.mytextview);
		    myTextView.setText("Response:" + html);
	    }
    };
   }
   }

 

Probleemu atrisinaaju,var sleegt. Esmu gan parsteigts ka neviens neatbildeeja,laikam sajaa forumaa apgrozaas tikai Pascal zinaataaji...

Link to comment
Share on other sites

(..),laikam sajaa forumaa apgrozaas tikai Pascal zinaataaji...

Šitā gan nevajadzēja teikt.

Link to comment
Share on other sites

Guest
Slēgta tēma, pievienot komentāru nav iespējams.
 Share

×
×
  • Izveidot jaunu...