Android的 - GPS监听器 [英] Android - GPS Listener

查看:160
本文介绍了Android的 - GPS监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序其中包括一些活动。
我想从GPS( onLocationChanged onProviderDisabled onProviderEnabled onStatusChanged )无论在哪个活动的用户正在使用。

我应该在哪里实施 LocationListener的为了得到这样的行为?

这是我创建的类:

 公共类MyLocationListener实现LocationListener的{私人的LocationManager的LocationManager;
私人字符串供应商;    @覆盖
公共无效onLocationChanged(位置locationGPS){    //做一点事
}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    // TODO自动生成方法存根}@覆盖
公共无效onProviderEnabled(字符串提供商){
    Log.i(===========================,=============== ===============);
    Log.i(onProviderEnabled,==============================);
    Log.i(===========================,=============== ===============);}@覆盖
公共无效onProviderDisabled(字符串提供商){    Log.i(===========================,=============== ===============);
    Log.i(onProviderDisabled,==============================);
    Log.i(===========================,=============== ===============);
}


解决方案

在你想要启动GPS活动启动位置监听器。要启动GPS可以使用

 的LocationManager的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener的LocationListener的=新CTLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,1.0F,LocationListener的);

和停止GPS可以使用

  locationManager.removeUpdates(LocationListener的);

及其更好编写GPS的开始对的onCreate /调用onStart和GPS除去上的服务的的onDestroy并使用该服务。否则,一旦你停止了GPS开始GPS的机会又在一些设备不足50%。

I am developing an Android application which consists a few activities. I want to get updates from the GPS (onLocationChanged, onProviderDisabled, onProviderEnabled, onStatusChanged) no matter in which activity the user is currently using.

Where should I implement the LocationListener in order to get such a behaviour?

This is the class I have created:

public class MyLocationListener implements LocationListener{

private LocationManager locationManager;
private String provider;

    @Override
public void onLocationChanged(Location locationGPS) {

    //do something
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    Log.i("===========================", "==============================");
    Log.i("onProviderEnabled", "==============================");
    Log.i("===========================", "==============================");

}

@Override
public void onProviderDisabled(String provider) {

    Log.i("===========================", "==============================");
    Log.i("onProviderDisabled", "==============================");
    Log.i("===========================", "==============================");
}

解决方案

Start the location listener on the activity you wants to start the gps. To start the GPS you can use

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new CTLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);

and to stop the GPS you can use

locationManager.removeUpdates(locationListener);

Its better you write the GPS start on the onCreate/onStart and the GPS remove on the onDestroy of a service and use that service. Otherwise once you stop the GPS the chance of starting the GPS again is less than 50% in some devices.

这篇关于Android的 - GPS监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆