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 7040965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:58:51+00:00 2026-05-28T01:58:51+00:00

Hai am trying to stop and Start the Gps location.when my Application is Start

  • 0

Hai am trying to stop and Start the Gps location.when my Application is Start the Gps is Started.In my broadCast receiver i need to stop the gps and again start it.But my Gps is not Stopped .Anybody please rectify help me.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    Log.i("ConnTest",locationManager.toString());
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
    System.out.println("Provider " + provider + " has been selected.");
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    final   double  a=(int) (location.getLatitude() * 1E6);
 final  double  b=(int) (location.getLongitude() * 1E6);
str = Double.toString(a);
str2=Double.toString(b);

} else {
    latituteField.setText("provider not available");
    longitudeField.setText("provider not available");
}

    txtNetworkInfo = (TextView)findViewById(R.id.txtNetworkInfo);

    receiver = new ConnectivityReceiver();

            registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));


}
@Override
protected void onResume() {
    super.onResume();
    //locationManager.requestLocationUpdates(provider,1000, 1, this);
    locationManager.requestLocationUpdates(
            provider, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            this
    );
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    longitudeField.setText(String.valueOf(lng));
    latituteField.setText(String.valueOf(lat));


}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Disenabled provider " + provider,
            Toast.LENGTH_SHORT).show();

}
protected void Display(Cursor c) {
    Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
     "Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
     Toast.LENGTH_LONG, 0).show();    
}

@Override
    protected void onDestroy() {
            unregisterReceiver(receiver);

            super.onDestroy();
    }

private String getNetworkStateString(NetworkInfo.State state){
    String stateString = "Unknown";

    switch(state)
    {
            case CONNECTED:         stateString = "Connected";break;
            case CONNECTING:        stateString = "Connecting";break;
            case DISCONNECTED:      stateString = "Disconnected"; break;
            case DISCONNECTING:     stateString = "Disconnecting";break;
            case SUSPENDED:         stateString = "Suspended";  break;
            default:                        stateString = "Unknown";break;
    }

    return stateString;
}

    private class ConnectivityReceiver extends BroadcastReceiver{

            private Timer mTimer;
            private TimerTask mTimerTask;

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                    if(null != info)
                    {
                            String state = getNetworkStateString(info.getState());
                            if(state.equals("Connected")){
                                mTimer = new Timer();
                                mTimerTask = new TimerTask() {
                                    @Override
                                    public void run() {
                                        MainActivity.this.runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                locationManager.removeUpdates(this);
                                                stop();
                                            }
                                        });

                                   }
                                };
                                mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
                            }


                            }
            }   
                    }

            }
    private void stop() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    }
                });

           }
        };
        mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);

    }
    }
  • 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-05-28T01:58:52+00:00Added an answer on May 28, 2026 at 1:58 am

    You should be disabling the updates when onPause() is called. When you press back your gps isn’t disabled? is there maybe another app or service running that is using GPS?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hai am trying to get the gps location using BroadCast Receiver.its working fine,But i
Hai i developed a application using broadcast receiver.I need to wake my Application always.so
Hai is it possible to check the broadcast receiver is currently running or not?if
Hai I have an application which is designed in netbeans6.0.1 where i need to
Hai am trying to set alarm for every 10 minutes.But its running first time
I am trying to do a simple application using Ajax and servlet. But when
Hai am trying to add the height and width of swf dynamically but i
Hai am trying to access the aspx variable.But i can't to do this.I dunno
Hai am new in android Application,am trying to get the latitude and longtitude using
Hai am Calling the webservice in my android application am using android version(r-18).but i

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.