android 模拟器 获得GPS

  首先需要在模拟器中手动添加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;


    /** Called when the activity is first created. */
    @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" />

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值