问题描述
我是新来的机器人。
我想获得GPS定位在一个广播接收器,但它显示了一个错误。
我的code是:
公共无效的onReceive(上下文的背景下,意图意图){
LocationManager locManager =(LocationManager)
getSystemService(Context.LOCATION_SERVICE);
在getSystemService方法//错误
LocationListener的locListener =新MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,
locListener);
位置LOC = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(** **的位置,位置+ loc.getLatitude());
}
问题:
- 是否有可能获得GPS定位数据广播接收器?
- 我那么另一种替代方式远是使用它是由广播接收器调用的服务。该服务可以获取GPS数据,但我怎样才能得到它在广播接收器?
从BroadcastReceiver的我无法启动位置管理为好。于是,我开始了服务上的BroadcastReceiver,并在该服务中我操纵的位置管理。我想我找到这个解决方案在Android开发文档。您也可以启动,而不是服务活动。
下面是切换到服务上的BroadcastReceiver的code:
在的onReceive
写这个方法
意图serviceIntent =新的意图(背景下,MyService.class);
serviceIntent.putExtra(locateRequest,locateRequest); //如果你想传递参数,从这里服务
serviceIntent.putExtra(queryDeviceNumber,结果[1]);
context.startService(serviceIntent); //开始为GET位置服务
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?
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
Intent serviceIntent = new Intent(context,MyService.class);
serviceIntent.putExtra("locateRequest", "locateRequest"); // if you want pass parameter from here to service
serviceIntent.putExtra("queryDeviceNumber", results[1]);
context.startService(serviceIntent); //start service for get location
这篇关于获取GPS定位在一个广播接收器/或服务,以广播接收器的数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!