2016-11-10 128 views
1

我只是想听听GPS状态变化......我有以下几点:GPS状态监听

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 

    lm.addGpsStatusListener(new android.location.GpsStatus.Listener() { 

     public void onGpsStatusChanged(int event) { 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      switch (event) { 
       case GPS_EVENT_STARTED: 
        if((alertDialog != null) && (alertDialog.isShowing())){ 
         alertDialog.dismiss(); 
        } 
        contador=0; 
        darHandle(); 
        break; 
       case GPS_EVENT_STOPPED: 
        if(mProgressDialog!=null && mProgressDialog.isShowing()) { 
         mProgressDialog.dismiss(); 
        } 
        contador=0; 
        break; 
      } 
     } 
    }); 

它可以很好地在我的Moto G的第一次,但它已被弃用,不工作在一些设备中...

我试图找出如何使用“onProviderEnabled /无效”在那种情况下......我已经尝试了许多代码示例,但并没有得到它的工作...

我正在试试这个:

LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 

     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationManager locationManager = (LocationManager) this 
      .getSystemService(Context.LOCATION_SERVICE); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 12000, 0, locationListenerGps); 

但在最后一行,它说:

无法解析法 'requestLocationUpdates(java.lang.String中,INT,INT,com.google.android.gms.location.LocationListener)'

我对Android/Java编程来说很新,任何人都可以帮助我?

回答

0

这只是发生在我身上。

我认为你的问题必须与导入的监听器的类有关。

有两种:

一个你使用:com.google.android.gms.location.LocationListener

和:android.location.LocationListener

您使用属于第二个这些方法。 使用那个,你应该会好起来的。

在Android中通常会发生这样的事情,所以要警惕,对于不同的API有很多类,其中一些重复名称,你必须尝试组合。