启用GPS状态/禁用广播接收器 [英] Gps status enabled/disabled broadcast receiver

查看:166
本文介绍了启用GPS状态/禁用广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米试图注册的广播接收器接收更新时,GPS状态发生改变。

I m trying to register a broadcast receiver to receive updates when gps status is changed.

不过,我GpsChangeReceiver的onReceive方法似乎并没有被调用时,GPS状态从启用已禁用,反之亦然改变。

However, my GpsChangeReceiver onReceive method doesn't seem to be called when gps status is changed from enabled to disabled or vice-versa.

首先,我注册reciever:

First of all, i'm registering the reciever:

      GpsChangeReceiver m_gpsChangeReceiver = new GpsChangeReceiver();
      this.registerReceiver(m_gpsChangeReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

然后,我有我的GPS接收器

Then , i have my gps Receiver

   public class GpsChangeReceiver   extends BroadcastReceiver
{   
  @Override
  public void onReceive( Context context, Intent intent )
  {
      final LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
          if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
         //do something
            }
         else
         {
            //do something else
         }
  }
}

最后,我的清单包含:

Finally, my manifest contains:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

任何人都可以明白为什么GPS接收器不会被调用,当我从电话s设定??

Can anyone see why the gps receiver is never called when i modify its status from the phone s settings??

推荐答案

两种方式注册的广播接收器在Android中的

  1. 在code(你的情况)
  2. 在AndroidManifest.xml中

让我来解释的差异。

您将只能接收广播只要背景在这里你注册你的接收器的活着。 因此,当活动应用程序 (这取决于你在哪里注册的接收器)的您不会收到广播了。

You will only receive broadcasts as long as context where you registered your receiver is alive. So when activity or application is killed (depending where you registered your receiver) you won't receive broadcasts anymore.

我猜你在活动方面这是不是很不错的方法注册的广播接收器。 如果你想注册的广播接收器,您的应用程序上下文你可以做这样的事情:

I guess you registered broadcast receiver in the activity context which is not very good approach. If you want to register broadcast receiver for your application context you can do something like this:

getApplicationContext().registerReceiver(m_gpsChangeReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

案例2(广播接收器在AndroidManifest.xml中注册的)

将接收广播当你的应用程序的 (系统会唤醒你的应用的)。这是正确的做法,当你想接收广播无论如果您的应用程序是运行

Case 2 (Broadcast Receiver registered in AndroidManifest.xml)

You will receive broadcasts even when your application is killed (system will wake your application up). This is right approach when you want to receive broadcasts no matter if your app is running.

将此接收到您的Andr​​oidManifest.xml:

Add this receiver to your AndroidManifest.xml:

<receiver android:name="com.yourpackage.NameOfYourBroadcastReceiver">
  <intent-filter>
    <action android:name="android.location.PROVIDERS_CHANGED" />
  </intent-filter>
</receiver>

编辑: 一些特殊节目的(即SCREEN_ON或SCREEN_OFF)的必须在code注册的(案例1)的,否则他们不会被发送到您的应用程序。

Some special broadcasts (i.e. SCREEN_ON or SCREEN_OFF) must be registered in code (case 1) otherwise they won't be delivered to your application.

这篇关于启用GPS状态/禁用广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆