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

  • Home
  • SEARCH
  • 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 8509423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:25:50+00:00 2026-06-11T03:25:50+00:00

What should i do to turn off the gps if location is not found

  • 0

What should i do to turn off the gps if location is not found at 1st instance..without location update being called

package mobile.sales;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

public class test extends Activity 
{
     DataSource dataSource;
     Double lat=0.0,lng=0.0;
     String GPS_FILTER = "";
     Thread triggerService;
     LocationListener locationListener;
     LocationManager lm;
     private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters
     private static final long MINIMUM_TIME_BETWEEN_UPDATES = 29*1000*60; // in Milliseconds
     protected LocationManager locationManager;
     boolean isRunning = true;
     NotificationManager notificationManager;
     PendingIntent contentIntent;
     Notification notification;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        turnGPSOn();
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
    }

     @Override
     public void onDestroy() 
     {
         // TODO Auto-generated method stub
         locationManager.removeUpdates(locationListener);
         locationManager= null;
         turnGPSOnOff();  
         super.onDestroy();
         // removeGpsListener();
     }

     private class MyLocationListener implements LocationListener
     {
        public void onLocationChanged(Location location) 
         {
            lat=location.getLatitude();
            lng=location.getLongitude();
            turnGPSOnOff();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            String currentDateandTime = sdf.format(new Date());     
            SimpleDateFormat sdft = new SimpleDateFormat("hh:mm:ss");
            String time=sdft.format(new Date());
            dataSource.insertIntoLocationDetails(""+lat,""+lng,currentDateandTime,""+time);
            Log.d("service","location Inserted");
         }

         public void onStatusChanged(String s, int i, Bundle b) {

         }

         public void onProviderDisabled(String s) {
         }

         public void onProviderEnabled(String s) {
         }
     }
     private void turnGPSOnOff(){
         Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
         intent.putExtra("enabled", false);
         sendBroadcast(intent);
     }
     private void turnGPSOn(){
            String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if(!provider.contains("gps")){
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3"));
                sendBroadcast(poke);
            }
     }
}
  • 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-11T03:25:52+00:00Added an answer on June 11, 2026 at 3:25 am

    You are calling the function when you are trying to DESTROY The activity itself. First find the Fix of GPS using GPSStatusListener

    Use this code

    Check Gps Status

    Call your GpsTurnOff status in GPSStatusListener

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

Sidebar

Related Questions

What should we use in jboss-log4j.xml in order to turn on/off traces for our
I have written a Windows service which should turn the display OFF when certain
I've been told not to use global variables. I use one to turn off/on
How to turn off PHP error formatting for getting only plain text? This should
According to php.net I should use mysql_real_escape_string() and turn off magic quotes , because
Should I turn off interrupt? How many situation I should concern? Is there any
How can I turn off session persistence for geronimo? Is this something I should
So I've been getting this feeling that I should turn off lazy loading, for
It should turn this int Yada (int yada) { return yada; } into this
Should I turn on Generate Debug Symbols in Xcode for release configuration? When it

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.