public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

((TextView) this.findViewById(R.id.textView)).setText("Something else");

// LocationManager locationManager = (LocationManager)
// getSystemService(Context.LOCATION_SERVICE);
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
// 0, 0, this);

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

String mocLocationProvider = LocationManager.GPS_PROVIDER;
locationManager.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);

try {

List data = new ArrayList();
InputStream is = getAssets().open("data.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {

data.add(line);
}
Log.e(LOG_TAG, data.size() + " lines");

new MockLocationProvider(locationManager, mocLocationProvider, data).start();

} catch (IOException e) {

e.printStackTrace();
}
}

public class MockLocationProvider extends Thread {

private List data;

private LocationManager locationManager;

private String mocLocationProvider;

private String LOG_TAG = "faren";

public MockLocationProvider(LocationManager locationManager,
String mocLocationProvider, List data) throws IOException {

this.locationManager = locationManager;
this.mocLocationProvider = mocLocationProvider;
this.data = data;
}

@Override
public void run() {

for (String str : data) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();
}

// Set one position
String[] parts = str.split(",");
Double latitude = Double.valueOf(parts[0]);
Double longitude = Double.valueOf(parts[1]);
Double altitude = Double.valueOf(parts[2]);
Location location = new Location(mocLocationProvider);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(altitude);

Log.e(LOG_TAG, location.toString());

// set the time in the location. If the time on this location
// matches the time on the one in the previous set call, it will be
// ignored
location.setTime(System.currentTimeMillis());

locationManager.setTestProviderLocation(mocLocationProvider,
location);
}
}
以上就是 Android location provider mock 从文本调用数据

更多相关文章

  1. Android(安卓)WebView使用全面解析(加载网络资源、本地HTML,JS交互
  2. Android中利用Intent传递数据到另一个页面
  3. android 调用 Menu 两种方法
  4. [置顶] android调用系统相机拍照并保存在本地
  5. android kernel 初始化 2
  6. Android(安卓)数据库SQLite的操作总结
  7. Android(安卓)3D 旋转的三角形(四)
  8. android 滑动加载数据
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. Android Junit 单元测试、异步测试方法简
  2. 通過XML設置屏幕方向(android:screenOrie
  3. Android源码阅读分析:从Activity开始(二)—
  4. 【Android】Uri、UriMatcher、ContentUri
  5. android滑动组件嵌套一般思路
  6. Android中Fragment的应用(android官方教程
  7. 技术---Java
  8. Android 开发之Matrix图片处理类的使用
  9. Android(安卓)4G模块调试总结
  10. Android(2)—Mono For Android App版本自动