您的需求分为两部分:
首先是方向变化,在AndroidManifest.xml中,为你的Activity注册方向的配置变化:
<activity android:name=".YourActivity"
android:configChanges="orientation">
在YourActivity 中,覆盖onConfigurationChanged 方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
// Do your stuffs here as LANDSCAPE mode
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//Do your stuffs here as PORTRAIT mode
}
super.onConfigurationChanged(newConfig);
}
第二部分,位置变更,将你的Activity注册为LocationListener并实现onLocationChanged方法:
@Override
public void onLocationChanged(Location loc) {
// Location changed as loc, do things here
}
在你的onCreate:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG, "GPS is enabled");
GPSListener = new MyLocationListener("GPS");
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
1, this);
} else {
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG, "NETWORK is enabled");
NetworkListener = new MyLocationListener("NETWORK");
manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 1, this);
}
}
希望这会有所帮助。