打开/关闭gps时如何触发广播接收器?

 端庄的一白_167 发布于 2023-02-09 12:23

当用户想要在打开/关闭位置提供时触发任何操作时,这非常有用

您应该在清单中添加此操作


添加此操作后,您可以触发广播接收器


    
        
        
    

在你的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的任何位置,而不仅仅是在活动或片段内.我希望它有所帮助.

3 个回答
  • 当用户想要在打开/关闭位置提供时触发任何操作时,这非常有用

    您应该在清单中添加此操作

    <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);
            }
        }
    }
    

    2023-02-09 12:26 回答
  • 您可以尝试使用此代码,因为用户@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";
          }
        }
    

    2023-02-09 12:26 回答
  • 我想添加@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的任何位置,而不仅仅是在活动或片段内.我希望它有所帮助.

    2023-02-09 12:26 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有