当用户想要在打开/关闭位置提供时触发任何操作时,这非常有用
您应该在清单中添加此操作
添加此操作后,您可以触发广播接收器
在你的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); } } }
@AndroidGuru如果打开/关闭位置模式,此代码如何告诉您? (13认同)
为什么我需要在这里实现LocationListener? (3认同)
除了广播接收者的Intent过滤器之外,何处添加<action android:name =“ android.location.PROVIDERS_CHANGED” /> (3认同)
pRaNaY.. 34
我想添加@Deepak Gupta的 答案.我想添加代码,以便在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));
这可用于您可以访问Context的任何位置,而不仅仅是在活动或片段内.我希望它有所帮助.
当用户想要在打开/关闭位置提供时触发任何操作时,这非常有用
您应该在清单中添加此操作
<action android:name="android.location.PROVIDERS_CHANGED" />
添加此操作后,您可以触发广播接收器
<receiver android:name=".GpsLocationReceiver"> <intent-filter> <action android:name="android.location.PROVIDERS_CHANGED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
在你的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); } } }
您可以尝试使用此代码,因为用户@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"; } }
我想添加@Deepak Gupta的 答案.我想添加代码,以便在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));
这可用于您可以访问Context的任何位置,而不仅仅是在活动或片段内.我希望它有所帮助.