首先需要在模拟器中手动添加GPS信息,有两种手动添加方法
1、在eclipse下,windows-->open perspective-->DDMS-->Emulator control-->Manual下手动设置经纬度,并按send按钮。
2、在cmd下手动添加信息。
(1)首先打开模拟器,然后运行cmd,输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不一样哈,具体端口号,模拟器左上方有显示的),这样会出现
Android Console: type 'help' for a list of commands
OK的字样。
如果是使用WIN7的朋友,控制台可能会提示telnet无效什么的,那是因为WIN7下默认是不出现telnet的,需要手动打开。具体为:[1]控制面板-->程序-->打开或关闭Windows功能,然后将Telnet服务器和Telnet客户端勾选上。[2]然后在管理工具-->服务中手动启动Telnet
(2)使用geo命令模拟发送GPS信号:
geo fix 经度 纬度
(3)这时就会发现在模拟器的状态栏上多了一个GPS的标志.
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 TT extends Activity {
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;
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("您所在位置为:");
}
}
xml文件中加一个权限 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />