public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
} else {
Toast.makeText(context, "not in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
@Override
public void onLocationChanged(Location arg0) {
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我需要在打开/关闭gps时触发广播接收器.调查此事并建议在app中实施最好的一个.
我正在使用以下代码来获取位置开/关事件.
<receiver
android:name=".receivers.GpsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
我正在开发基于地理围栏的应用程序.基于仅在需要时重新注册地理围栏,我们必须在应用程序收到GEOFENCE_NOT_AVAILABLE警报后重新注册地理围栏.这通常发生在禁用NLP(Android的网络位置提供程序)之后.
通过使用此广播接收器,我在启用Android的网络位置提供程序时重新注册了地理围栏.
但是从API级别26开始,这个广播接收器永远不会工作.请参阅 后台执行限制.
那么如何在API 26及更高版本中实现相同的任务呢?
注意:即使app在后台,我也需要重新注册地理围栏.