我意识到GDK还没有发布,但我已经开始尝试在谷歌鼓励的情况下在Glass上安装APK(https://developers.google.com/glass/gdk)。我写了一个非常简单的慢跑应用程序,可以通过LocationManager计算距离和速度。有人知道目前这是否应该与Glass原生配合使用(即无需与手机配对)吗?从玻璃拆卸物上的读数来看,它似乎有一个内置的GPS芯片。不过,我在从Glass获得GPS时遇到了问题,我感觉它目前受到了限制。
是的,但诀窍是需要使用requestLocationUpdates()而不是getLastKnownLocation()。这就是让我感到困惑的部分,因为如果我只是获取LastKnownLocation(),我最终会得到null。
因此,使用Compass示例作为基础(以避免其他潜在问题和工作),如果你正确运行了它,这里有一个非常基本的破解方法来扩展它并添加位置更新:
-
如果你像我一样使用安卓工作室,遇到"未引用符号"错误(像我一样),并且懒得用不同的方式解决它(我认为你看到了趋势…),那么提前定义一些事情:
private LocationManager mLocationManager; private LocationListener mLocationListener; private Criteria criteria;
-
更新onKeyDown()方法
... String directionName = mSpokenDirections[getDirectionIndex(heading)]; String headingText = String.format(speechFormat, (int) heading, directionName); // **** Location hack starts here... **** criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); provider = mLocationManager.getBestProvider(criteria, true); boolean isEnabled = mLocationManager.isProviderEnabled(provider); if (isEnabled) { headingText += " and GPS is enabled on provider " + provider; // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; // Register the listener with the Location Manager to receive location updates mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener); } else { headingText = ", but GPS not enabled on provider " + provider; } // **** Location hack ends here **** mSpeech.speak(headingText, TextToSpeech.QUEUE_FLUSH, null); return true; ...
在这里,它只是添加了文档中的LocationListener片段:http://developer.android.com/guide/topics/location/strategies.html,并添加一些条件。你可以在提供商中进行硬编码,但我想使用Glass报告的"最佳",所以它(希望)能适应不同的场景(还没有验证,所以目前只是一种希望)。
-
添加一个简单的makeUseOfLocation()方法来说明细节:
public void makeUseOfNewLocation(Location location) { if (location != null) { String lat = "Latitude: " + location.getLatitude(); String lng = ", Longitude: " + location.getLongitude(); String summary = lat + lng; mSpeech.speak(summary, TextToSpeech.QUEUE_FLUSH, null); } return; }
这样你就可以四处走动,听听它认为你在哪里。我在大楼里转了一圈,只是为了好玩
-
将适当的权限添加到您的清单
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
生成并运行。。。我在手机上搭配和不搭配Glass(XE9更新)的情况下都试过了,一切似乎都如预期。