获取广播接收器中的 GPS 位置/或广播接收器数据传输服务
我是 Android 新手。
我想在广播接收器中获取 GPS 位置,但它显示错误。
我的代码是:
public void onReceive(Context context, Intent intent) {
LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// errors in getSystemService method
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locListener);
Location loc = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(" **location**", " location" + loc.getLatitude());
}
问题:
- 是否可以在广播接收器中获取 GPS 位置数据?
- 到目前为止我尝试过的另一种替代方法是使用广播接收器调用的服务。该服务可以获取 GPS 数据,但如何在广播接收器中获取它?
I am new to android.
I want to get GPS Location in a broadcast receiver but it shows an error.
My code is :
public void onReceive(Context context, Intent intent) {
LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// errors in getSystemService method
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locListener);
Location loc = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(" **location**", " location" + loc.getLatitude());
}
Questions :
- Is it possible to get GPS Location data in Broadcast receiver?
- Another alternative way I tried so far was to use a service which is invoked by the Broadcast receiver. The service can get GPS data, but how can I get it in the Broadcast receiver?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,两者都有可能。
您的服务带有计时器,可以在一段时间内向位置接收器发送请求:
您的接收器带有侦听器。侦听器可以在您的活动中使用,以通知您新位置已准备好:
侦听器的接口:
在您的活动中仅获取一个点:
编辑:
如果您想在您的位置启动服务活动:
类似地,您可以在服务中定义一个侦听器来接收接收器找到的位置
编辑
在清单中添加以下行
yes both of them are possible.
your service with a timer to send request to location receiver in periods of time:
your receiver with a listener. listener can be used in your activity to be notified that a new location is ready for you:
an interface for listener:
in your activity for getting just one point:
edit:
if you want to start service in your activity:
similarly you can define a listener in your service to receive locations found by receiver
Edit
Add the following lines in Manifest
从 BroadcastReceiver 我也无法启动位置管理器。因此,我在 BroadcastReceiver 上启动了一项服务,并在该服务中操作了位置管理器。我想我在Android开发文档中找到了这个解决方案。您还可以启动 Activity 而不是 Service。
下面是在 BroadcastReceiver 上切换到 Service 的代码:
将其写入
onReceive
方法From BroadcastReceiver I could not start Location Manager as well. So I started a service on BroadcastReceiver and in that service I manipulated Location Manager. I think I found this solution in Android development documentation. You also can start Activity instead of Service.
Here is the code of switch to Service on BroadcastReceiver:
write this in
onReceive
method第 1 步:
打开 AndroidManifest.xml 并添加广播接收器。
第 2 步: 创建具有对话框主题的活动:
第 3 步:
创建一个名为 GpsConnectorReceiver 的 BroadcastReceiver 类
第 4 步: 创建另一个名为 ConnectivityCheck 的服务类:
第 5 步: 创建一个活动调用 ActivityDialogGps
STEP 6: 当 Gps 连接关闭时 ActivityDialogGps 调用并显示对话框:
STEP 1:
Open AndroidManifest.xml and add the broadcast receiver.
STEP 2: create activity with dialog theme:
STEP 3:
Make a BroadcastReceiver Class named GpsConnectorReceiver
STEP 4: Make another service class named ConnectivityCheck :
STEP 5: create a activity called ActivityDialogGps
STEP 6: When Gps connection is turned off ActivityDialogGps called and show the dialog: