Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:22:07+00:00 2026-06-01T09:22:07+00:00

I have a thread that sends GPS coordinates to a database every six seconds

  • 0

I have a thread that sends GPS coordinates to a database every six seconds and I have a check that verifies that the user is within a defined area. If the user is not within the location, I want an alert dialog that notifies them that they are out of range, and if they are within the area I want a dialog that tells them they are within range.

I have the checks working properly, but I have tried and I’m pretty sure that I can’t add the dialog on the background thread. I have read a bit about using handlers. How can I implement one?

This is how I call FindLocation.java from my main activity (MainActivity.java):

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

Below is 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();
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:22:08+00:00Added an answer on June 1, 2026 at 9:22 am

    It is a little difficult to read the entire code, but I will show you how to display an AlertDialog from a background Thread:

    Create a handler inside onCreate(), or onResume()… something that runs on the UI-Thread:

    ...onCreate(){
          //...
          mHandler=new Handler();
    }
    

    Then inside your Thread() just use:

    mHandler.post(new Runnable() {
        public void run(){
            //Be sure to pass your Activity class, not the Thread
            AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
            //... setup dialog and show
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread that needs to be executed every 10 seconds. This thread
I want to have a data connection that sends every 60 seconds the geo-position.
I have created a windows service that sends email reminders every 30 seconds. 10
I have a service that runs this thread to get GPS coordinates LocationManager lm
We have a thread program that sends bulk mail. The information like 1. To
I have a thread reading data from a bluetooth stream that sends the data
I have a C++ process which has a thread that needs to send floats
I have one thread that writes results into a Queue. In another thread (GUI),
I have a thread that, when its function exits its loop (the exit is
I have a thread that appends rows to self.output and a loop that runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.