Tee*_*kam 42 gps android broadcastreceiver location-provider
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我需要在打开/关闭gps时触发广播接收器.调查此事并建议在app中实施最好的一个.
Dee*_*pta 72
当用户想要在打开/关闭位置提供时触发任何操作时,这非常有用
您应该在清单中添加此操作
<action android:name="android.location.PROVIDERS_CHANGED" />
Run Code Online (Sandbox Code Playgroud)
添加此操作后,您可以触发广播接收器
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
在你的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
pRa*_*NaY 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
}
}
};
Run Code Online (Sandbox Code Playgroud)
对于片段:
getContext().registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
Run Code Online (Sandbox Code Playgroud)
对于活动:
registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
Run Code Online (Sandbox Code Playgroud)
这可用于您可以访问Context的任何位置,而不仅仅是在活动或片段内.我希望它有所帮助.
Igo*_*sky 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";
}
}
Run Code Online (Sandbox Code Playgroud)