- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我无法使用下面的代码从 GPS 接收任何数据,而且我不确定自己做错了什么,似乎我的代码与我在网上看到的所有内容都匹配。我想最终将它添加到后台服务以记录 gps 坐标,即使 Activity 不可见或其他应用程序打开并且速度大于某个定义的数量,但此时我不能甚至可以显示任何数据。我是 android 的新手,但我不知道我做错了什么?
public class MainActivity extends Activity {
TextView textView;
MyLocationListener myLocationListener;
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView = (TextView)findViewById(R.id.message);
addLocationListener();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void addLocationListener()
{
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
final String PROVIDER = lm.getBestProvider(c, true);
this.myLocationListener = new MyLocationListener();
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0F, this.myLocationListener);
//lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0.0F, myLocationListener);
Log.d("LOC_SERVICE", "Service RUNNING!");
}
public void updateLocation(Location location)
{
double latitude, longitude;
latitude = location.getLatitude();
longitude = location.getLongitude();
int mph = convertSpeed(location.getSpeed());
Log.d("LOC_SERVICE", "mph: "+mph);
this.textView.setText(this.textView.getText()+"\n"+"lat: "+latitude+" lng: "+longitude+" mph: "+mph);
}
private int convertSpeed(float speed) {
int mph =(int)(2.236936D * speed);
return mph;
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
Log.d("LOC_SERVICE", "Listener RUNNING!");
updateLocation(location);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}
最佳答案
您需要先使用权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
接下来你在 onCreate() 方法中使用下面的代码来访问 GPS。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 1, this);
}
现在,如果 GPS 未启用,则需要引用以下代码。详细解释可以引用how to get[DEAD LINK] [current location in android] 1教程。
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
这里我们使用 intents 将用户重定向到位置设置页面,以便他可以在位置设置下启用 GPS。
关于android - 如何使用 Android LocationManager 和 Listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21085497/
我正在尝试捕捉评论状态事件,例如有人正在关闭它。并在从插件中获得任何生命迹象之后。向日志文件(从任何日志级别)或 STDOUT 发送一行。atlassian-plugins.xml 看起来像这样:
我在跑; sass --watch --style compressed --sourcemap css/sass:css 我得到了; NameError: uninitialized constan
您好,我正在创建一个 android 应用程序作为 ejabbered 服务器的 XMPP 客户端。 但我真的很困惑,因为我看到我可以将消息作为数据包或消息发送,我也可以使用 PacketListen
我有一个 imageview - 它的属性 -focusable 和 focusableintouchmode 都设置为 true 我已经在我的 Activity 中实现了 onFocus
我有一个查询来查找指定条形码的文档 ID: Future findBarcode() async { String searchBarcode = await BarcodeScanner.sca
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
这可能是一个非常基本的问题,但我就是不明白。使用 Express.js 创建应用和启动应用监听 1234 端口有什么区别,例如: var express = require('express'); v
所以我想知道什么是更好看的解决方案/有什么区别以及在决定制作多个监听器(1 个监听器用于 1 个按钮)还是仅 1 个 ActionListener 用于 GUI 中的所有按钮(大约 10 个按钮),并
我目前正在尝试制作一个聊天室服务器。我正在努力做到这一点,以便我可以同时监听新请求的连接和监听从已建立的连接发送的消息。 我可以用它来监听请求的连接: def reqlisten(): glo
有没有办法在事件监听器方法中访问类上下文并有可能删除监听器? 示例 1: import {EventEmitter} from "events"; export default class Event
应用程序在本地主机上工作正常。但是当它连接到服务器时出现错误。 我通过端口 22 连接服务器 这是错误 Error: listen EADDRNOTAVAIL Error: listen EADDRN
我有类似的东西: $scope.$on(config.SOME_CONSTANT, ()=> { activate(); // plus a bunch of instantiatio
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我构建了一个自定义属性并将其添加到可观察列表中。但是,如果属性内容发生更改,则不会调用任何监听器。以下代码片段向您展示了“建筑”: public static final class TestObje
这里我不明白这两种方法的基本区别是什么。 var events = require('events'); var eventEmitter = new events.EventEmitter(); v
我正在尝试使用 grunt-express 设置 Grunt 来启动我的 Express 服务器。读完docs后和 this SO question ,我还是想不通。我已经为我的 Grunt 文件尝试
如果这里问题的某些方面不清楚,我深表歉意,因为我是 Node 和 javascript 的新手。请询问更多详情 我有一个使用 socketio 连接到 firebase 的 Node 应用程序。在 h
情况 我可能没有使用传统意义上的 PHPUnit。我正在使用带有 Selenium 2 的 PHPUnit。我们有这个想法来记录 Selenium 以“重现步骤”的方式执行的操作。这意味着如果我们调用
我正在尝试学习在 struts2 中使用 session 。所以,我只想实现一个登录/注销、配置文件应用程序。我正在关注互联网上提供的基本教程。但是,它根本不起作用。请帮助我解决以下问题如何解决。 S
我是一名优秀的程序员,十分优秀!