GPS状态发生变化时如何接收系统广播?状态、发生、系统、GPS

2023-09-07 04:18:21 作者:人生苦短,必须性感

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

I wrote the code below, but it's not working. Can anybody help me? I just want to passively receiving GPS status changes, rather than proactive inquiries. Saving power is most important.

没有消息输出.

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:

Here is my 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>

推荐答案

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

I believe you are simply pointing to the wrong location in the manifest, change the receiver's name:

<receiver android:name=".GPStStatusReceiver">

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

This type of receiver is great for starting an app whenever the GPS is enabled, but while the app is running the LocationListener's onProviderEnabled() or onProviderDisable() will catch these even if the refresh interval is set to 10 days and a 1000 miles, or more. So you will not necessarily waste battery power if you pass generous settings to the requestLocationUpdates() method.

评论补充

我只能猜测您没有收到 GPS_ENABLED_CHANGE,因为您没有触发位置请求.简单地通过单击位置菜单中的复选框启用 GPS 功能不会广播此意图,某些应用程序必须请求当前位置.此外,我没有找到关于此 Intent 的任何官方文档,这意味着 Android可能会随时更改或删除它.

I can only guess that you are not receiving the GPS_ENABLED_CHANGE because you are not triggering a location request. Simply enabling the GPS feature by clicking the checkbox in the Locations menu will not broadcast this Intent, some app must request the current location. Also, I didn't find any official documentation on this Intent which means Android might change or remove this at any time.

也许你想要官方支持的LocationManager.PROVIDERS_CHANGED_ACTION,这会广播一个Intent当 GPS 提供商(和其他提供商)启用/禁用时.

Perhaps you want the officially supported LocationManager.PROVIDERS_CHANGED_ACTION, this will broadcast an Intent when the GPS provider (and other providers) is enabled / disabled.

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