首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GPS停止,onPause()

GPS停止,onPause()
EN

Stack Overflow用户
提问于 2011-04-30 00:25:51
回答 1查看 1.8K关注 0票数 0

在我的应用程序中,当我开始使用全球定位系统时,我无法停止它,不管我是按下后退还是主键,吐司都会出现(我在requestLocationUpdate调用中放了0,0来看看它是否能做到这一点)

它表明removeUpdates调用的onPause方法(当我按下back/home按钮进入时)不做任何事情。或者是其他的问题?请帮助我,因为在过去的3天里,我一直被这个问题困扰着。这个应用程序的逻辑是:我有一个按钮,我按下它,然后它会检查哪个提供者是活动的whatIsEnabled()方法

我的代码是:

代码语言:javascript
复制
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();
}
EN

回答 1

Stack Overflow用户

发布于 2011-10-29 03:39:27

我开发了一个类似的应用程序,但我将

locationManager.removeUpdates(locationListener);

在获取location之后,在onCreate()方法中,而不是在onPause()中。将它放在onPause()中的问题是,当您离开(暂停)活动时,它将被调用。因为我没有跟踪用户的移动,所以一旦我得到他们的位置,我就停止监听器。

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

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5834667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档