GPS stopping onPause()

Tutorials that use the MapActivity. Many using GPS functionality.

GPS stopping onPause()

Postby CyberGriZzly » Fri Apr 29, 2011 5:28 pm

Hello everyone,

in My app when I start using GPS I can`t stop it, it doesn`t matter if I pus the back or home button the Toast appears (I put 0, 0 at the requestLocationUpdate call to see if it does that)
It apears that the onPause method (in which when I pus the back/home button enters) the removeUpdates call doesn`t do enything. Or something else is the problem? please help me cause for the last 3 days I`m stuck with this problem.The logic of the app: I have a button which I pus then it checks which provider is active whatIsEnabled() method
My code is:

Code: Select all
public class Map extends MapActivity {
   private MapView mapView;
   private MapController mc;
   private GeoPoint p;
   private Context context;
   private Drawable drawable;
   private Bundle instanceState;
   private EditText et;
   private ImageButton connect, layers, location;
   private int i = 1;
   //private Settings settings;
   private LocationManager locationManager;
   private LocationListener locationListener;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instanceState = savedInstanceState;
        setContentView(R.layout.main);
        context = getApplicationContext();
       
        //settings = new Settings();
       
        //to zoom
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
       
        et = (EditText) findViewById(R.id.address);
       
        connect = (ImageButton) findViewById(R.id.connect_icon);
        connect.setOnClickListener(connect_button);
       
       
        layers = (ImageButton) findViewById(R.id.layers); 
        layers.setOnClickListener(layer_button);
           
        //locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //locationListener = new Coordonates(drawable, mapView, Map.this);
        location = (ImageButton) findViewById(R.id.location_icon);
        location.setOnClickListener(location_button);
              
       
       
        //markers
        drawable = this.getResources().getDrawable(R.drawable.blue_pin);
       
    }
   
    private OnClickListener connect_button = new OnClickListener() {
      
      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         Intent myIntent = new Intent(Map.this, Connection.class);
         Map.this.startActivity(myIntent);
      }
   };
   
    private OnClickListener layer_button = new OnClickListener() {
      
      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         
         if(i%2 != 0) {
            mapView.setStreetView(true);
            mapView.setSatellite(false);
         }
         else {
            mapView.setSatellite(true);
            mapView.setStreetView(false);
         }
         i++;
      }
   };
   
   private OnClickListener location_button = new OnClickListener() {
      
      @Override
      public void onClick(View v) {
         // TODO Auto-generated method stub
         
         //registerLocListener();
         
         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           locationListener = new Coordonates(drawable, mapView, Map.this);
          
           switch(Settings.whatIsEnabled()) {
           case 1: {
              Toast.makeText(context, "Location services disabled",Toast.LENGTH_LONG).show();
              break;
           }
           case 2: {
              Toast.makeText(context, "Location obtained via GPS satellites",Toast.LENGTH_LONG).show();
              Toast.makeText(context, "Waiting for location",Toast.LENGTH_LONG).show();
              System.out.println("1");
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);//50
              if(Coordonates.getLatitude() == 0)   
                 Toast.makeText(context, "No GPS signal",Toast.LENGTH_LONG).show();
              System.out.println("2");
              break;
           }
           case 3: {
              Toast.makeText(context, "Location obtained via Wi-Fi/mobile network",Toast.LENGTH_LONG).show();
              locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);//1000
              if(Coordonates.getLatitude() == 0)   
                 Toast.makeText(context, "No Wi-Fi/mobile network signal",Toast.LENGTH_LONG).show();
              break;
           }/*
           case 4: {
              Toast.makeText(context, "Location obtained via the best provider available",Toast.LENGTH_LONG).show();
              locationManager.
              break;
           }*/
           }
      }
   };
   /*
   protected void onResume() {
      
      super.onResume();
   }*/
   
   @Override
   protected void onPause() {
      // TODO Auto-generated method stub
      System.out.println("a intrat in onPause()");
      if(locationManager != null) {
         locationManager.removeUpdates(locationListener);
      }
      locationListener = null;
      locationManager = null;
      super.onPause();
   }
CyberGriZzly
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 31, 2011 11:13 pm

Top

Re: GPS stopping onPause()

Postby onlineadr » Wed May 04, 2011 9:12 am

i would suggest that you use a service...
onlineadr
Experienced Developer
Experienced Developer
 
Posts: 59
Joined: Thu Apr 28, 2011 12:45 pm

Top

Return to Map Tutorials

Who is online

Users browsing this forum: Ghizlane and 1 guest