android 模拟器上GPS的使用
来源:互联网 发布:云计算开发工程师 编辑:程序博客网 时间:2024/05/21 18:06
Java代码:
package test.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.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class TestGPSActivity extends Activity{private static final String TAG = "GPSTestActivity";private Location preLocation;private long preTime;private static final int step = 2000; // msecsprivate LocationManager locationManager;private boolean status = false;private Button btn;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.i(TAG, "onCreate(Bundle savedInstanceState)");btn = (Button) findViewById(R.id.beginBtn);btn.setOnClickListener(new OnClickListener(){public void onClick(View v){if (!status){status = true;startLoactionService();btn.setText("点击这里停止");}else{status = false;stopLocationService();btn.setText("点击这里开始");}}});}public void startLoactionService(){String serviceName = Context.LOCATION_SERVICE;locationManager = (LocationManager) getSystemService(serviceName);// String provider = LocationManager.GPS_PROVIDER;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);Log.i(TAG, "we choose:"+provider);Location location = locationManager.getLastKnownLocation(provider);Log.i(TAG, "we :"+location);preTime = System.currentTimeMillis();updateWithNewLocation(location);locationManager.requestLocationUpdates(provider, step, 0, locationListener);}public void stopLocationService(){if (locationManager != null){locationManager.removeUpdates(locationListener);}}private final LocationListener locationListener = new LocationListener(){public void onLocationChanged(Location location){Log.i(TAG, "onLocationChanged(Location location)");updateWithNewLocation(location);}public void onProviderDisabled(String provider){updateWithNewLocation(null);}public void onProviderEnabled(String provider){}public void onStatusChanged(String provider, int status, Bundle extras){}};private void updateWithNewLocation(Location location){Log.i(TAG, "updateWithNewLocation(Location location)");String latLongString;TextView myLocationText;myLocationText = (TextView) findViewById(R.id.result);if (location != null){double lat = location.getLatitude();double lng = location.getLongitude();long subTime = (System.currentTimeMillis() - preTime) / 1000;float v = (subTime == 0 || preLocation == null) ? 0 : (preLocation .distanceTo(location) / subTime);latLongString = "纬度:" + lat + " 经度:" + lng + " 速度:" + v + " m/s , " + v * 3.6 + " km/h";preLocation = location;preTime = System.currentTimeMillis();}else{latLongString = "无法获取地理信息";}myLocationText.setText(System.currentTimeMillis() + ": " + latLongString);}}
xml代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/beginBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start" /><TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="data" /></LinearLayout>
加上权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
GPS数据模拟方法:
打开DDMS视图,找到Emulator Control,在Manual里面选择Sexagesimal,输入Longitude和Latitude,点击Send.至此完成测试.
- android 模拟器上GPS的使用
- android 模拟器上GPS的使用
- Android 模拟器的上使用GPS的bug
- Android模拟器上进行GPS的模拟操作法
- android平台上GPS信号的获取和展示(三)之配置与模拟器设置
- android模拟器定位GPS
- 在Android模拟器上怎样模拟GPS功能
- android 模拟器 获取gps 值
- android模拟器GPS信号设置
- android模拟器模拟定位GPS
- android模拟器模拟定位GPS
- android模拟器设置GPS信息
- android模拟器模拟定位GPS
- android的模拟器中得到GPS的值
- 在Windows系统上使用Android模拟器的方法
- 使用adb卸载安装在Android模拟器上的程序
- 使用file explorer管理android模拟器上的文件
- 在Win10上使用Visual Studio2015的Android模拟器
- 关于HTTP协议中的KeepAlive属性
- 双口RAM在高速数据采集系统中的应用
- c++中List在遍历时删除节点的方法,带源码
- 学生信息管理系统总结之“经典语句一”
- AVR-GCC Makefile 中文翻译并加上我的理解(原发于:2007-11-20)
- android 模拟器上GPS的使用
- 使用远程桌面时发生AtBroker.exe错误的解决方法
- ZOJ-2008-一个最短路问题-(dijkstra+heap,spfa)
- Oracle 不同故障的恢复方案
- Jamendo开源在线音乐播放器源码分析(一)
- 【hdu】 Nightmare
- 20条编程经验
- Zend API:深入 PHP 内核
- 用form提交不同编码的数据及在IE下解决办法