我在下面编写了代码,但它没有用.有谁能够帮我?我只想被动地接收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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
我相信你只是指向清单中的错误位置,更改接收者的名字:
<receiver android:name=".GPStStatusReceiver">
Run Code Online (Sandbox Code Playgroud)
这种类型的接收器非常适合在启用GPS时启动应用程序,但在应用程序运行时,LocationListener的onProviderEnabled()或onProviderDisable()将捕获这些,即使刷新间隔设置为10天和1000英里,或者更多.因此,如果将大量设置传递给requestLocationUpdates()方法,则不一定会浪费电池电量.
来自评论的补充
我只能猜测你没有收到,GPS_ENABLED_CHANGE因为你没有触发位置请求.只需通过单击"位置"菜单中的复选框启用GPS功能,就不会广播此Intent,某些应用必须请求当前位置.此外,我没有找到任何关于此Intent的官方文档,这意味着Android可能随时更改或删除此文件.
也许你想要官方支持的LocationManager.PROVIDERS_CHANGED_ACTION,这将在启用/禁用GPS提供商(和其他提供商)时广播一个Intent.
<action android:name="android.location.PROVIDERS_CHANGED" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8643 次 |
| 最近记录: |