如何一次性取消GPS广播? [英] How to catch GPS off broadcast once?

查看:57
本文介绍了如何一次性取消GPS广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上网冲浪了,但没有找到解决问题的方法.

I have surfed the web and I haven't found a solution to my problem.

在我的Android应用程序中,每次用户关闭GPS时,我都必须捕获并向服务器发送通知.这时候我已经写了这段代码

In my android app I have to catch and send a notification to the server everytime the user turn off the GPS. At this time I have writed this code

在Android机器人中:

In the Android manifiest:

    <receiver android:name="proguide.prosegur.scr.BL.receivers.GPSStatusBroadcastReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>

在GPSStatusBroadcastReceiver类中:

In the GPSStatusBroadcastReceiver class:

public class GPSStatusBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
        // here I have to send the notification
    }
}

问题在于,每次用户放下GPS时,都会使用相同的Context和Intent参数使该函数调用两次(我一次只能发送1条通知).

The problem is that everytime the user put down the GPS, I get this function called twice with identical Context and Intent arguments (I can only send 1 notification at a time).

重要说明:它必须在API级别8下工作.

那么,为什么会发生两次呢?我该怎么做(做对了,不弄乱代码)一次只发送1条通知?谢谢,对不起,我的英语.

So, why this happen twice? What can I do (doing it right, not messing up the code) to send only 1 notification at a time? Thanks, sorry for my English.

推荐答案

尝试一下:

public class GpsReceiver extends BroadcastReceiver {        
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            final String action = intent.getAction();
            if (action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                // GPS is switched off.
                if (!context.getSystemService(Context.LOCATION_SERVICE).isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    // Do something.
                }
            } 
        }
    }
}

此外,您应该使用Android提供的变量 LocationManager.PROVIDERS_CHANGED_ACTION ,而不是对"android.location.PROVIDERS_CHANGED" 进行硬编码.

Also, instead of hardcoding "android.location.PROVIDERS_CHANGED", you should use the variable LocationManager.PROVIDERS_CHANGED_ACTION provided by Android.

不要在 AndroidManifest.xml 文件中设置GPS接收器,而是通过 Service 注册GPS接收器,如下所示:

Instead of setting your GPS receiver in your AndroidManifest.xml file, register your GPS receiver via a Service as follow:

public class GpsService extends Service {
    private BroadcastReceiver mGpsReceiver;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       super.onStartCommand(intent, flags, startId);
       registerReceiver();
       return Service.START_NOT_STICKY;
    }
    private void registerReceiver() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            IntentFilter mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
            this.mGpsReceiver = new GpsReceiver();
            this.registerReceiver(this.mGpsReceiver, mIntentFilter);
        }       
    }
}

这篇关于如何一次性取消GPS广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆