如何触发广播接收器在GPS开启/关闭?
问题描述 投票:35回答:5
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) {
}
}
在我的应用程序,我需要触发广播接收器在GPS开启/关闭。关注此事,并建议在应用程序以实现最好的一个。
android
gps
broadcastreceiver
location-provider
5个回答
71
投票
当用户想触发反过来任何行动开/关的位置提供,这是有用
你应该在清单中添加这个动作
并添加这个动作后,你可以触发你的广播接收器
而在你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);
}
}
}
31
投票
我想补充@Deepak古普塔answer。我想为当GPS状态改变如何在您的片段或活动注册一个动态brodacsast接收器添加代码。
在您的活动或片段如下注册您的广播接收器。
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
}
}
};
对于片段:
getContext().registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
对于活动:
registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
这在使用,你必须访问上下文,不只是一个活动或片段内的任何地方。我希望它的帮助。
13
投票
你可以试试这个代码,用户@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";
}
}
0
投票
后,Android定位模式工作的解决方案在Android中引入
我们可以使用位置模式检查更精确
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;
}
}
-2
投票
你可以试试这个
表现
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();
}
热门问题