Android gps打开/关闭时如何触发广播接收器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20673620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger broadcast receiver when gps is turn on/off?
提问by Teekam
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) {
}
}
In my app , i need to trigger broadcast receiver when gps is turn on/off. look into the matter and suggest best one to implement in app.
在我的应用程序中,我需要在 gps 打开/关闭时触发广播接收器。调查此事并建议在应用程序中实施的最佳方案。
回答by Deepak Gupta
This is useful when user want to trigger any action on turn On/Off location provides
当用户想要触发打开/关闭位置提供的任何操作时,这很有用
You should add this action in manifest
您应该在清单中添加此操作
<action android:name="android.location.PROVIDERS_CHANGED" />
and after add this action you can trigger your broadcast receiver
添加此操作后,您可以触发广播接收器
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And in your BroadcastReceiver
class you have to implement LocationListener
in that class which is given following below..
在你的BroadcastReceiver
班级中,你必须LocationListener
在下面给出的那个班级中实现..
public class GpsLocationReceiver 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);
}
}
}
回答by pRaNaY
I want to add @Deepak Guptaanswer. I want to add code for how to register a dynamic brodacsast receiver in your fragmentor activitywhen GPS status changed.
我想添加@Deepak Gupta答案。我想添加有关如何在 GPS 状态更改时在片段或活动中注册动态 brodacsast 接收器的代码。
Register your broadcast receiver as below in your activity or fragment.
在您的活动或片段中注册您的广播接收器,如下所示。
private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
//Do your stuff on GPS status change
}
}
};
For fragment:
对于片段:
getContext().registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
For activity:
对于活动:
registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
This an be used any place where you have access to a Context, not just inside an activity or fragment. I hope its helps.
这可以在您有权访问上下文的任何地方使用,而不仅仅是在活动或片段内部。我希望它有帮助。
回答by IgorGanapolsky
You can try this code, as user @DanielNugent pointed out:
你可以试试这个代码,正如用户@DanielNugent 指出的那样:
ContentResolver contentResolver = getContext().getContentResolver();
// Find out what the settings say about which providers are enabled
int mode = Settings.Secure.getInt(
contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (mode != Settings.Secure.LOCATION_MODE_OFF) {
if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
locationMode = "Device only. Uses GPS to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
}
}
回答by Vinayak
Working solution after Android location modes are introduced in Android
Android 中引入 Android 定位模式后的工作解决方案
We can check with location mode for more accurate
我们可以通过定位模式查看更准确
private boolean isGpsEnabled(Context context) {
ContentResolver contentResolver = getContext().getContentResolver();
// Find out what the settings say about which providers are enabled
// String locationMode = "Settings.Secure.LOCATION_MODE_OFF";
int mode = Settings.Secure.getInt(
contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (mode != Settings.Secure.LOCATION_MODE_OFF) {
return true;
/* if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
locationMode = "Device only. Uses GPS to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
}*/
} else {
return false;
}
}
回答by Maher Tag
you can try this
你可以试试这个
Manifest
显现
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.location.GPS_ENABLED_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MyReceiver
我的接收器
if (intent.getAction().matches("android.location.GPS_ENABLED_CHANGE"))
{
boolean enabled = intent.getBooleanExtra("enabled",false);
Toast.makeText(context, "GPS : " + enabled,
Toast.LENGTH_SHORT).show();
}