1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 定位 广播 android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...

android 定位 广播 android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...

时间:2019-04-15 06:45:08

相关推荐

android 定位 广播 android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...

如何触发广播接收器在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();

}

热门问题

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。