2011-04-29 58 views
0

在我的应用程序,当我开始使用GPS我无法阻止它,没关系,如果我推回或主页按钮吐司出现(我把0,0在requestLocationUpdate调用看看它是否如此)GPS停止,onPause()

它apears onPause方法(其中当我按下后退/ home按钮进入)removeUpdates调用不起作用。或者别的什么是这个问题?请帮助我导致过去3天内我遇到了这个问题。该应用的逻辑:我有一个按钮,用于我推然后它检查其提供者是活性whatIsEnabled()方法

我的代码是:

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(); 
} 
+0

你是否在控制台中输出“onPause()中的intrat”?检查控制台并回复 – Vivek 2011-04-29 17:33:53

回答

0

我开发了类似的应用,但我把

locationManager.removeUpdates(locationListener);

在onCreate()方法后,我得到的位置,而不是onPause()。将它放入onPause()的问题在于当您离开(暂停)该活动时它将被调用。由于我没有跟踪用户的移动,所以一旦我找到他们的位置,我就会停止听众。

这可能不是有用的,你正在开发与我不同的功能的应用程序。

祝你好运!