2014-12-06 83 views
0

我创建了一个类来使用LocationListener的,但我得到ClassCastException异常时,我请求位置更新使用Android GPS进行位置更新?

这是我的类代码

public class Loc implements 
    LocationListener, GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener { 
// A request to connect to Location Services 
private LocationRequest mLocationRequest; 

// Stores the current instantiation of the location client in this object 
private LocationClient mLocationClient; 
/* 
* Note if updates have been turned on. Starts out as "false"; is set to 
* "true" in the method handleRequestSuccess of LocationUpdateReceiver. 
*/ 
boolean mUpdatesRequested = false; 

// Milliseconds per second 
public static final int MILLISECONDS_PER_SECOND = 1000; 

// The update interval 
public static final int UPDATE_INTERVAL_IN_SECONDS = 10; 

// A fast interval ceiling 
public static final int FAST_CEILING_IN_SECONDS = 1; 

// Update interval in milliseconds 
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = MILLISECONDS_PER_SECOND 
     * UPDATE_INTERVAL_IN_SECONDS; 

// A fast ceiling of update intervals, used when the app is visible 
public static final long FAST_INTERVAL_CEILING_IN_MILLISECONDS = MILLISECONDS_PER_SECOND 
     * FAST_CEILING_IN_SECONDS; 

Location currentLocation ; 
LocationListener locListener; 
Context x; 

public Loc(Context con) { 
    // Create a new global location parameters object 
    this.x=con; 
    mLocationRequest = LocationRequest.create(); 


    /* 
    * Set the update interval 
    */ 
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 

    // Use high accuracy 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    // Set the interval ceiling to one minute 
    mLocationRequest 
      .setFastestInterval(FAST_INTERVAL_CEILING_IN_MILLISECONDS); 

    // Note that location updates are off until the user turns them on 
    mUpdatesRequested = false; 

    /* 
    * Create a new location client, using the enclosing class to handle 
    * callbacks. 
    */ 
    mLocationClient = new LocationClient(con, this, this); 

} 

public void stop() { // If the client is connected 
    if (mLocationClient.isConnected()) { 
     stopPeriodicUpdates(); 
    } 

    // After disconnect() is called, the client is considered "dead". 
    mLocationClient.disconnect(); 

} 

public void start() { 
    mLocationClient.connect(); 

} 

public void startUpdates() { 
    mUpdatesRequested = true; 

    startPeriodicUpdates(); 
} 

public Location getLocation() { 

    // Get the current location 
    currentLocation = mLocationClient.getLastLocation(); 

    // Display the current location in the UI 
    Log.e("lat log", "" + currentLocation.getLatitude() + " , " 
      + currentLocation.getLongitude()); 
    return currentLocation; 
} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onConnected(Bundle arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onDisconnected() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    currentLocation=location; 
    Log.e("lat log", "" + currentLocation.getLatitude() + " , " 
      + currentLocation.getLongitude()); 
} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

private void stopPeriodicUpdates() { 
    mLocationClient 
      .removeLocationUpdates((com.google.android.gms.location.LocationListener) this); 
} 

private void startPeriodicUpdates() { 

    mLocationClient.requestLocationUpdates(mLocationRequest,(com.google.android.gms.location.LocationListener) this); 
} 

}

这是我的活动代码

public class MainActivity1 extends Activity { 
Loc l; 
TextView t; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_activity1); 
    t=(TextView)findViewById(R.id.txt); 
    l = new Loc(getApplicationContext()); 
    l.start(); 
} 

public void getLoc(View v) { 
    // l.getLocation(); 
    l.startUpdates(); 
} 

我得到我的拉特和长得很好。 但我的问题是,与位置更新

我得到这个错误

   Caused by: java.lang.ClassCastException: com.example.gps.Loc cannot be cast to com.google.android.gms.location.LocationListener 

我做得到,我不能通过LocationListener的这样

mLocationClient.requestLocationUpdates(mLocationRequest,(com.google.android.gms.location.LocationListener) this); 

但我应该怎样准确地传递给它?

回答

4

只需要添加接口

com.google.android.gms.location.LocationListener 
+0

工作真棒!完美的伴侣!谢谢 – TejjD 2015-07-21 07:39:19

+0

这可以帮助:http://stackoverflow.com/questions/28261754/where-should-i-request-location-updates-in-a-service – samaniego 2016-01-29 05:24:39

0

这样。

public class Loc implements 
GooglePlayServicesClient.ConnectionCallbacks 
,GooglePlayServicesClient.OnConnectionFailedListener 
,com.google.android.gms.location.LocationListener { 
    //... 
}