【问题标题】:How do I receive the system broadcast when GPS status has changed?GPS状态发生变化时如何接收系统广播?
【发布时间】:2012-07-09 15:54:25
【问题描述】:

我写了下面的代码,但它不起作用。有谁能够帮我?我只想被动接收GPS状态变化,而不是主动查询。省电最重要。

没有消息输出。

package com.sharelbs.lbs.service;  
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class GPStStatusReceiver extends BroadcastReceiver {
  public static final String GPS_ENABLED_CHANGE_ACTION = "android.location.GPS_ENABLED_CHANGE";
  @Override
    public void onReceive(Context context, Intent intent) {
      Log.d("---------log--------","GPS Status onReceive");
      if(intent.getAction().equals(GPS_ENABLED_CHANGE_ACTION)){
        Log.d("---------log--------","GPS Status Changed");
        startMyProgram();
      }
    }
}

这是我的 Manifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name=".service.GPStStatusReceiver"> 
  <intent-filter> 
    <action android:name="android.location.GPS_ENABLED_CHANGE" /> 
  </intent-filter> 
</receiver>

【问题讨论】:

    标签: android gps


    【解决方案1】:

    我相信你只是在清单中指向错误的位置,更改接收者的名称:

    <receiver android:name=".GPStStatusReceiver">
    

    这种类型的接收器非常适合在启用 GPS 时启动应用程序,但在应用程序运行时,即使刷新间隔设置为 10 天和 1000,LocationListener 的 onProviderEnabled() 或 onProviderDisable() 也会捕获这些英里,或更多。因此,如果您将大量设置传递给 requestLocationUpdates() 方法,则不一定会浪费电池电量。

    来自 cmets 的添加

    我只能猜测您没有收到GPS_ENABLED_CHANGE,因为您没有触发位置请求。简单地通过单击位置菜单中的复选框来启用 GPS 功能不会广播此 Intent,某些应用程序必须请求当前位置。另外,我没有找到关于这个 Intent 的任何官方文档,这意味着 Android might change or remove this at any time

    也许您想要官方支持的LocationManager.PROVIDERS_CHANGED_ACTION,这将在启用/禁用 GPS 提供程序(和其他提供程序)时广播一个 Intent。

    <action android:name="android.location.PROVIDERS_CHANGED" />
    

    【讨论】:

    • 我的路径是正确的。 android:name=".service.GPStStatusReceiver"。它也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多