在Android模拟器中模拟GPS
2012-01-25 14:35
323 查看
package org.exapmle.locationtest;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocationTestActivity extends Activity {
/** Called when the activity is first created. */
TextView myLocationText;
TextView myLongitude;
TextView myLatitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName=Context.LOCATION_SERVICE;
// dumpProviders();
locationManager=(LocationManager)this.getSystemService(serviceName);
//查询条件
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//选择最佳的提供者
String provider=locationManager.getBestProvider(criteria,true);
//查询上一个已知的位置
Location location=locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
//设置监听器,自动更新的最小时间为间隔1秒,最小位移变化超过5米
locationManager.requestLocationUpdates(provider, 1000, 5, locationListener);
}
private final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location) {
updateWithNewLocation(location);}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
private void updateWithNewLocation(Location location){
myLocationText = (TextView)this.findViewById(R.id.myLocationText);
myLongitude = (TextView)this.findViewById(R.id.myLongitude);
myLatitude = (TextView)this.findViewById(R.id.myLatitude);
if(location!=null){
myLongitude.setText(String.valueOf(location.getLongitude()));
myLatitude.setText(String.valueOf(location.getLatitude()));
}else{
myLongitude.setText("No GPS");
}
myLocationText.setText("您所在位置为:");
}
}
GPS测试
如果是在模拟器中调试的话,有二种方法可以进行GPS测试。
第一:通过DDMS。打开"Window->Show View->Other” 中打开"Emulator Control” View即可手动设置地理位置数据,如下图所示:
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocationTestActivity extends Activity {
/** Called when the activity is first created. */
TextView myLocationText;
TextView myLongitude;
TextView myLatitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName=Context.LOCATION_SERVICE;
// dumpProviders();
locationManager=(LocationManager)this.getSystemService(serviceName);
//查询条件
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//选择最佳的提供者
String provider=locationManager.getBestProvider(criteria,true);
//查询上一个已知的位置
Location location=locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
//设置监听器,自动更新的最小时间为间隔1秒,最小位移变化超过5米
locationManager.requestLocationUpdates(provider, 1000, 5, locationListener);
}
private final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location) {
updateWithNewLocation(location);}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
private void updateWithNewLocation(Location location){
myLocationText = (TextView)this.findViewById(R.id.myLocationText);
myLongitude = (TextView)this.findViewById(R.id.myLongitude);
myLatitude = (TextView)this.findViewById(R.id.myLatitude);
if(location!=null){
myLongitude.setText(String.valueOf(location.getLongitude()));
myLatitude.setText(String.valueOf(location.getLatitude()));
}else{
myLongitude.setText("No GPS");
}
myLocationText.setText("您所在位置为:");
}
}
GPS测试
如果是在模拟器中调试的话,有二种方法可以进行GPS测试。
第一:通过DDMS。打开"Window->Show View->Other” 中打开"Emulator Control” View即可手动设置地理位置数据,如下图所示:
![](http://hi.csdn.net/attachment/201105/23/0_1306134661M9N0.gif)
相关文章推荐
- android模拟器模拟定位GPS
- Android模拟器中模拟GPS、打电话、…
- 在Android模拟器中模拟GPS、打电话、发短信……
- Android模拟器中模拟GPS、打电话、发短信
- 在Android模拟器中模拟GPS、打电话、发短信等操作
- android模拟器Genymotion模拟GPS
- android模拟器模拟定位GPS
- 在Android模拟器中模拟GPS、打电话、发短信……
- Android模拟器上进行GPS的模拟操作法
- 在Android模拟器中模拟GPS、打电话、发短信……
- Android_android模拟器模拟定位GPS
- android模拟器模拟定位GPS
- 在Android模拟器中模拟GPS、打电话、发短信……
- 在Android模拟器上怎样模拟GPS功能
- Android模拟器上模拟GPS
- 在Android模拟器上模拟GPS功能总是null的解决方法
- Android模拟器利用GPS获取手机位置
- 利用android模拟器模拟任意号码来电
- arcgis for android 学习 - (7) 在真机上 模拟GPS
- Android平台5款GPS导航软件横向对比测试----纸上谈兵-模拟导航