我如何收到系统广播时,GPS状态发生了变化? [英] How do I receive the system broadcast when GPS status has changed?

查看:98
本文介绍了我如何收到系统广播时,GPS状态发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了code以下,但它不工作。任何人可以帮助我吗?我只想被动地接收GPS状态的改变,而不是主动查询。省电是最重要的。

目前没有消息输出。

 包com.sharelbs.lbs.service;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;

公共类GPStStatusReceiver扩展的BroadcastReceiver {
  公共静态最后弦乐GPS_ENABLED_CHANGE_ACTION =android.location.GPS_ENABLED_CHANGE;
  @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
      Log.d(--------- --------日志,GPS状态的onReceive);
      如果(intent.getAction()。等于(GPS_ENABLED_CHANGE_ACTION)){
        Log.d(--------- --------日志,GPS状态更改);
        startMyProgram();
      }
    }
}
 

下面是我的Manifest.xml:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_UPDATES/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
<接收机器人:名称=。service.GPStStatusReceiver>
  <意向滤光器>
    <作用机器人:名称=android.location.GPS_ENABLED_CHANGE/>
  &所述; /意图滤光器>
< /接收器>
 

解决方案

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

 <接收机器人:名称=。GPStStatusReceiver>
 

这类型的接收器是伟大的开始每当GPS启用一个应用程序,但在应用程序运行LocationListener的的onProviderEnabled()或onProviderDisable()将赶上这些即使刷新间隔设置为10天,1000英里,或更多。所以,你不一定会浪费电池电源如果你通过大手笔设置的requestLocationUpdates()方法。

添加自评

我只能猜测,你是不是收到 GPS_ENABLED_CHANGE 因为你没有触发的位置请求。只需通过单击复选框中的位置菜单将不直播这个意图使GPS功能,有些应用程序必须要求的当前位置。另外,我没有找到这个意图的任何正式文件,这意味着的Andr​​oid可能会改变或消除这种在任何时候

也许你想正式支持的<一个href="http://developer.android.com/reference/android/location/LocationManager.html#PROVIDERS_CHANGED_ACTION">LocationManager.PROVIDERS_CHANGED_ACTION,这将播出一个Intent当GPS提供商(和其他供应商)的启用/禁用。

 &lt;作用机器人:名称=android.location.PROVIDERS_CHANGED/&GT;
 

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.

There is no message output.

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();
      }
    }
}

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">

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.

Addition from comments

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.

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" />

这篇关于我如何收到系统广播时,GPS状态发生了变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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