[英]How to get GPS turn on-off events in android?
我有一個簡單的廣播接收器,可以接收GPS的打開/關閉。 當我打開GPS並出現“ Improve accuracy dialog?
(開始出現在android 8.0中)由Android框架提供,我兩次收到android.location.PROVIDERS_CHANGED
接收器。
以下是我的示例接收器。
public class LocationServicesChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"Intent action is "+intent.getExtras());
}
我在清單中聲明如下
<receiver
android:name=".geofence.core.LocationServicesChangedReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>
題:
onReceive()
有什么辦法可以停止接收多個onReceive():
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
{
Log.i(TAG, "Location Providers changed");
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//You receive multiple data from more than one Provider,
// choose which one you want to use (GPS or NETWORK Provier).
if (isGpsEnabled) {
Log.i(TAG, "Location Providers is GPS");
// put your code in here to get data from GPS PROVIDER.
}
else if (isNetworkEnabled) {
Log.i(TAG, "Location Providers is Network");
// put your code in here to get data from NETWORK PROVIDER.
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.