Par*_*ala 10 android location google-maps
我有一个Android应用程序,我需要用户模拟他们当前的位置.下面是我使用的代码,让用户按绿色按钮开始模拟他们的位置.
下面的代码在按下"绿色按钮"时开始伪造位置.
greenButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addTestProvider(LocationManager.GPS_PROVIDER,
"requiresNetwork" == "",
"requiresSatellite" == "",
"requiresCell" == "",
"hasMonetaryCost" == "",
"supportsAltitude" == "",
"supportsSpeed" == "",
"supportsBearing" == "",
Criteria.POWER_LOW,
Criteria.ACCURACY_FINE);
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
newLocation.setLatitude(fakeLocation.getLatitude());
newLocation.setLongitude(fakeLocation.getLongitude());
newLocation.setAccuracy(fakeLocation.getAccuracy());
newLocation.setTime(System.currentTimeMillis());
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,
LocationProvider.AVAILABLE,
null, System.currentTimeMillis());
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);
}
});
Run Code Online (Sandbox Code Playgroud)
当我在地图上放置标记并按下绿色按钮时.位置嘲笑开始.那个"假位置"成了我当前的位置.
但是在10-20秒之后,嘲笑就结束了.我的"真实位置"成为我当前的位置.我在网上看到了很多例子,他们使用我用来模拟位置的相同代码.我不知道为什么我的应用程序会发生这种情况.任何帮助,将不胜感激.
首先,位置请求将影响您对适当位置提供商的模拟位置。
如果您为 GPS 提供商创建并设置模拟位置,然后再次向 GPS 提供商发出位置请求,则模拟位置将持续存在,直到 GPS 硬件收到新位置。这可能需要 10-20 秒或更长时间(但是大约 10-20 秒后,嘲笑就结束了......)
这个问题也是一些假位置应用程序问题的答案。您可以看到诸如“使用此应用程序后我无法获取我的真实当前位置!!!”之类的评论 市场上。因为他们需要像您一样发出位置请求。
最后,请在服务中循环模拟操作。我建议在后台线程上执行此操作,也许每 1 或 2 秒一次。通过这种方式,您可以减少其他应用程序可能的位置请求的影响。
Handler mHandler;
private void loopMocking(){
mHandler.post(mMockRunnable);
}
private Runnable mMockRunnable = new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
newLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);
mHandler.postDelayed(mMockRunnable, 1000); // At each 1 second
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1143 次 |
| 最近记录: |