mar*_*337 5 android android-location kotlin android-maps-v2 android-gps
我想检测用户是否在运行时关闭了该位置。我可以检查他是否打开了它,或者在应用程序启动之前用户是否关闭了位置,但我无法检查他是否在之后关闭了它。
代码示例:
MapEntity扩展LocationListener
class MapViewer(a: MainActivity, parentView: ViewGroup) : MapEntity(a, parentView) {
override fun onProviderEnabled(provider: String?) {
activity.hideGpsSnackbar()
}
override fun onProviderDisabled(provider: String?) {
activity.showGpsSnackbar()
}
}
Run Code Online (Sandbox Code Playgroud)
对于实时 GPS 位置检查,我正在使用 GnssStatus.Callback()
更新:
我已经BroadcastReceiver根据下面的答案创建了。
abstract class GPSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
try {
val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManager
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
onGpsChanged(true)
} else {
onGpsChanged(false)
}
} catch (ex: Exception) {
App.log("IsGPSEnabled: $ex")
}
}
abstract fun onGpsChanged(isEnabled: Boolean)
}
Run Code Online (Sandbox Code Playgroud)
我的其中一项活动中的代码:
private val gpsStatusReceiver = object : GPSReceiver() {
override fun onGpsChanged(isEnabled: Boolean) {
if (isEnabled){
hideGpsSnackbar()
} else {
showGpsSnackbar()
}
}
}
override fun onStart() {
super.onStart()
registerReceiver(gpsStatusReceiver, IntentFilter())
}
override fun onStop() {
super.onStop()
unregisterReceiver(gpsStatusReceiver)
}
Run Code Online (Sandbox Code Playgroud)
更新
如果要支持 Android 6.0,则不能使用抽象类。因为它会尝试从 AndroidManifest 中定义的这个类中创建对象。Android 8.0+ 不会检查 AndroidManifest 中的接收器,因此您可以从抽象类中实例化对象。所以代替它创建接口。
我实际上是用BroadcastReceiver.
我可以分享我的代码;它是 Java,但我认为您可以轻松地将其转换为 kotlin。
Broadcastreceiver例子:
public class GPSBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//isGPSEnabled = true;
} else {
//isGPSEnabled = false;
}
}catch (Exception ex){
}
}
}
Run Code Online (Sandbox Code Playgroud)
BroadcastReceiver的添加到manifest例子:
<receiver android:name=".others.GPSBroadcastReceiver">
<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)
然后(就我而言)我ApplicationContext按如下方式管理它:
private GPSBroadcastReceiver gpsBroadcastReceiver = new GPSBroadcastReceiver();
@Override
public void onCreate() {
....
registerReceiver(gpsBroadcastReceiver, new IntentFilter());
}
@Override
public void onTerminate() {
...
unregisterReceiver(gpsBroadcastReceiver);
}
Run Code Online (Sandbox Code Playgroud)
这只是一个例子,可能还有其他方法,但实际上我对那个很好;祝你好运!
编辑:
尝试在您的清单中添加此权限
<uses-permission android:name="android.permission.ACCESS_GPS" />
Run Code Online (Sandbox Code Playgroud)
编辑:对于 Android 8.0+BroadcastReceiver像这样注册:
registerReceiver(gpsBroadcastReceiver,IntentFilter("android.location.PROVIDERS_CHANGED"))
Run Code Online (Sandbox Code Playgroud)
在里面添加动作是AndroidManifest行不通的。