2015-05-14 88 views
5

我可以在片段中將LocationListener與LocationManager一起使用嗎?實際上,當我使用它,它給了我在這行錯誤lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,(android.location.LocationListener)this);當我不蒙上第四參數android.location.LocationListener它給我的錯誤..片段中的位置監聽器

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 


public class Speedometer extends Fragment implements LocationListener { 

    TextView txt; 
    public Speedometer(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.speedometer, container, false); 

     txt=(TextView)rootView.findViewById(R.id.speedometer); 

     LocationManager lm= (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this); 

     return rootView; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     if(location==null){ 
      txt.setText("-.- m/s"); 
     } 
     else { 
      float nCurrentSpeed=location.getSpeed(); 
      txt.setText(nCurrentSpeed+"m/s"); 
     } 
    } 
} 
+0

使用不投'lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,這一點);' – Kushal

+0

不投它給了我錯誤 – Bisma

回答

12

我想你已經錯誤地執行com.google.android.gms.location.LocationListener接口,只有一個1抽象方法onLocationChanged

你應該而實現android.location.LocationListener有4種抽象方法

onLocationChanged(Location location)

onProviderDisabled(String provider)

onProviderEnabled(String provider)

onStatusChanged(String provider, int status, Bundle extras)

+0

謝謝你這麼多,我用它和我的錯誤被刪除 – Bisma

+0

酷!弄清楚什麼時候使用其中一種或者其他的有點令人費解...... – Supercelo