日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。