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();
}
熱門問題
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android 定位 广播,android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html:(1) 登录界面
- 下一篇: 频率域锐化滤波器