简体   繁体   English

在 Android 中,如何在设备位置发生变化时将广播发送到 BroadcastReceiver

[英]how to send Broadcast to BroadcastReceiver when location of device changes, in Android

In my android application, I want to sendBroadcast to BroadcastReceiver when the location of the device changes, even when my application is not running.在我的 android 应用程序中,即使我的应用程序没有运行,我也想在设备位置发生变化时将sendBroadcast发送到BroadcastReceiver I am new to android, please tell how can I do that?我是安卓新手,请告诉我该怎么做?

Try this尝试这个

    LocationManager manager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 manager.requestLocationUpdates("gps", 10, 10, new LocationListener() {
 @Override
 public void onLocationChanged(Location location) {
   sendBroadcast(rec); 
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {

 }

 @Override
 public void onProviderEnabled(String provider) {

 }

 @Override
 public void onProviderDisabled(String provider) {

 }
});

In order to broadcast in background you have to make use of AsyncTask or Service为了在后台广播,您必须使用 AsyncTask 或 Service

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在广播接收器中触发 BTLE 事件时获取设备位置? - How to get device location when a BTLE event triggers in broadcast receiver? 如何将广播消息发送到Android中的所有iOT设备? - How to send broadcast message to all iOT device in android? 如何在需要时在服务器上发送设备的位置 - How to send location of the device on server when needed 我的在线状态更改时,Android Broadcastreceiver崩溃 - Android Broadcastreceiver crashes when my onlinestate changes Android BroadcastReceiver in Service在向其发送广播时给出空指针异常 - Android BroadcastReceiver in Service giving null pointer exception when sending broadcast to it 如何从Android设备向所有其他参与者Android设备发送(广播)数据 - How to send(broadcast) data from android device to all other participant android device Android:一个用于多播的BroadcastReceiver - Android: one BroadcastReceiver for muliple broadcast 如何在启用BroadcastReceiver时获取粘性广播数据? - How to get sticky broadcast data when BroadcastReceiver is enabled? android BroadCastReceiver的位置 - android BroadCastReceiver for location Android服务出现故障时如何发送广播? - How to send broadcast when Android service goes down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM