【发布时间】:2020-05-07 05:49:23
【问题描述】:
我这里有小问题。
我已从用户那里取得所有必要的位置权限。
我创建了如下的广播接收器:
public class GpsLocationReceiver : BroadcastReceiver() {
private lateinit var mLocationManager: LocationManager
override fun onReceive(context: Context, intent: Intent) {
LogUtil.e(">>>On Receive","called")
mLocationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
LogUtil.e(">>>On Receive","called inside")
GetLocationAccessFragment().getAndSaveCurrentLocation()
}
}
}
在我的清单中,我做了如下:
<receiver android:name=".ui.user_profile.fragments.GetLocationAccessFragment$GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
现在,在我的片段 init() 方法中注册它:
mContext.registerReceiver(
GpsLocationReceiver(),
IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
并且,在工作完成后取消注册:
mContext.unregisterReceiver(GpsLocationReceiver())
但是,不幸的是,当我在设备中打开/关闭 GPS 时,没有调用 OnReceive() 方法。 可能是什么问题?
【问题讨论】:
标签: android kotlin gps provider android-gps