Alert Dialog-Thread - Android

Ich habe einen thread, der sendet die GPS-Koordinaten an eine Datenbank, die alle sechs Sekunden und ich habe einen check, der überprüft, dass der Benutzer innerhalb eines definierten Bereichs. Wenn der Benutzer nicht in der Lage, ich möchte eine Warnmeldung angezeigt, die Sie benachrichtigt, dass Sie außerhalb des Bereichs liegen, und wenn Sie innerhalb der Bereich, den ich will einen dialog, der Ihnen sagt, dass Sie in Reichweite sind. Ich habe die Prüfungen ordnungsgemäß funktioniert, aber ich habe versucht, und ich bin mir ziemlich sicher, dass kann ich nicht hinzufügen, der dialog auf der hintergrund-thread. Ich habe ein wenig gelesen über die Verwendung von Handlern, aber ich bin mir nicht sicher, wie Sie Sie umsetzen. Wenn Sie irgendwelche Vorschläge haben, würde ich mich freuen! Danke.

Dies ist, wie rufe ich FindLocation.java von meinem Haupt-Aktivität (MainActivity.java):

new FindLocation(getBaseContext()).start(usr_id1); //sends a user id with it

Unten ist FindLocation.java

public class FindLocation extends Thread {

public boolean inJurisdiction;
public boolean AlertNotice = false;
private LocationManager locManager;
private LocationListener locListener;

Context ctx;
public String userId;

public FindLocation(Context ctx) {
     this.ctx = ctx;
}

 public void start(String userId) {
        this.userId = userId;
        super.start();
      }

 @Override
public void run() {
     Looper.prepare();
    final String usr = userId;      

    //get a reference to the LocationManager
    locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

    //checked to receive updates from the position
    locListener = new LocationListener() {
        public void onLocationChanged(Location loc) {

            String lat = String.valueOf(loc.getLatitude()); 
            String lon = String.valueOf(loc.getLongitude());

            Double latitude = loc.getLatitude();
            Double longitude = loc.getLongitude();

            if (latitude >= 39.15296 && longitude >= -86.547546 && latitude <= 39.184901 && longitude <= -86.504288 || inArea != false) {
                Log.i("Test", "Yes");  

                inArea = true;

                JSONArray jArray;
                String result = null;
                InputStream is = null;
                StringBuilder sb = null;

                 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                 nameValuePairs.add(new BasicNameValuePair("id", usr));

                //http post
                try{

                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://www.example.com/test/example.php");     
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                     is = entity.getContent();
                     }catch(Exception e){
                         Log.e("log_tag", "Error in http connection"+e.toString());
                    }

                //convert response to string
                try{
                      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                       sb = new StringBuilder();
                       sb.append(reader.readLine() + "\n");

                       String line="0";
                       while ((line = reader.readLine()) != null) {
                                      sb.append(line + "\n");
                        }
                        is.close();
                        result=sb.toString();
                        }

                        catch(Exception e){
                              Log.e("log_tag", "Error converting result "+e.toString());


                        }

                try{
                      jArray = new JSONArray(result);
                      JSONObject json_data=null;
                      for(int i=0;i<jArray.length();i++){
                             json_data = jArray.getJSONObject(i);
                             String ct_name = json_data.getString("phoneID");
                             Log.i("User ID", ct_name);
                             if(ct_name == usr) {
                                 locManager.removeUpdates(locListener);
                             }
                             else{
                                 locManager.removeUpdates(locListener);
                                 Log.i("User ID", "NONE");
                             }
                         } 
                      }

                      catch(Exception e){
                            //Log.e("log_tag", "Error converting result "+e.toString());

                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://example.com/test/example.php");

                            try {
                                   List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(2);
                                   nameValuePairs1.add(new BasicNameValuePair("lat", lat)); 
                                   nameValuePairs1.add(new BasicNameValuePair("lon", lon));
                                   nameValuePairs1.add(new BasicNameValuePair("id", usr));
                                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
                                   httpclient.execute(httppost);
                                   Log.i("SendLocation", "Yes"); 
                             } 
                             catch (ClientProtocolException g) {
                                 //TODO Auto-generated catch block
                             } catch (IOException f) {
                                //TODO Auto-generated catch block
                                e.printStackTrace();
                            } 
                } 
            } 

            else {
                Log.i("Test", "No");
                inArea = false;
            }
        }
        public void onProviderDisabled(String provider){
        }
        public void onProviderEnabled(String provider){ 
        }
        public void onStatusChanged(String provider, int status, Bundle extras){
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 0, locListener);
    Looper.loop();
 }
}

InformationsquelleAutor mkyong | 2012-04-03

Schreibe einen Kommentar