打开/关闭wifi或GPS时,我的BroadcastReceiver被调用两次?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打开/关闭wifi或GPS时,我的BroadcastReceiver被调用两次?相关的知识,希望对你有一定的参考价值。

我已经google了很多时间来找到解决方案,但它没有用。多年来,如果你遇到这个问题并解决了它,请帮助我。

当我打开或关闭wifi或Gps时,Receiver会触发两次,但有时会触发一次,但如果触发两次,则会破坏我的应用程序计划。

提前thx ...

在manifest.xml中:

<receiver android:name=".MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

            <action android:name="android.location.PROVIDERS_CHANGED"/>
        </intent-filter>
    </receiver>

这是BroadcastReceiver类:

public class MyBroadcastReceiver extends BroadcastReceiver {
String TAG_NETW = "NetworkConnctivityUtils";


@Override
public void onReceive(Context context, Intent intentR) {
    try {
        //   MyApplication.dbApp = new SqliteHelper(context, ConstHelper.DATABASE_PATH, ConstHelper.DATABASE_NAME);
        SimpleDateFormat sdf = new SimpleDateFormat(ConstHelper.Simple_Date_Format);
        if (intentR.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);

                if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    Log.d(TAG_NETW, "locationGps" + " ------ event On " + intentR.getAction());

                    StatusDriverQueryHelper.insertStatusDriverlog(EnumDeviceStatus.GPSOn, "0", AuthenticateHelper.getDeviceId(context, ""), UnicodeHelper.numberToEnglish(sdf.format(new Date())));
                    StatusDriverQueryHelper.showItems();

                } else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    // todo : if at this time of other provider change the gps was of / or no what will be the status
                    Log.d(TAG_NETW, "locationGps" + " ------ event Off " + intentR.getAction());

                    StatusDriverQueryHelper.insertStatusDriverlog(EnumDeviceStatus.GPSOff, "0", AuthenticateHelper.getDeviceId(context, ""), UnicodeHelper.numberToEnglish(sdf.format(new Date())));
                    StatusDriverQueryHelper.showItems();

                }
                LogService.sendAllStatus();
            } else {

            }
    } catch (Exception e) {
        Log.d(TAG_NETW, "locationGps" + " error On GPS EVENT Reciver " + intentR.getAction());
    }

    /***************************/

    try {

        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intentR.getAction())) {
            int status = NetworkConnctivityUtils.getConnectivityStatusString(context);
            Log.e(TAG_NETW, " here : " + intentR.getAction() + " , STATUS : " + String.valueOf(status));
            // Wifi is connected
            if (status == NetworkConnctivityUtils.NETWORK_STATUS_NOT_CONNECTED) {
                Log.d(TAG_NETW, "CONNECTIVITY" + " Not-----Available");
            } else if (status == NetworkConnctivityUtils.NETWORK_STAUS_WIFI) {
                Log.d(TAG_NETW, "CONNECTIVITY" + " OK wifi");
            } else if (status == NetworkConnctivityUtils.NETWORK_STATUS_MOBILE) {
                Log.d(TAG_NETW, "CONNECTIVITY" + " OK mobile");
            } else {
                Log.d(TAG_NETW, "CONNECTIVITY" + " nonononon ---");
            }


        }
    } catch (Exception e) {

    }
}

}

*****找到解决问题的方法

但我不知道这是处理它的最佳方法:

我使用一个标志来检查它是否是第一次,然后在1或2秒之后返回标志。

// ....

    @Override
    public void onReceive(Context context, Intent intentR) {
         try {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstHelper.Simple_Date_Format);
        if (intentR.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);

             if (ConstHelper.GpsActionisDoneOnce == false) {
                 ConstHelper.GpsActionisDoneOnce = true;
                 new Handler().postDelayed(new Runnable() {
                     public void run() {
                        ConstHelper.GpsActionisDoneOnce = false;
                     }
                }, 500);
                      // GPS ACTION/CHECKING .. 
               } else {

               }
           }
       } catch (Exception e) {

      }

// ......

答案

不推荐使用广播接收器,因为系统性能较低。正确的方法是使用JobScheduler

以上是关于打开/关闭wifi或GPS时,我的BroadcastReceiver被调用两次?的主要内容,如果未能解决你的问题,请参考以下文章

Android - 以编程方式打开或关闭 GPS

如何防止用户关闭 GPS、Wifi 和蓝牙?

检测用户是不是打开或关闭了 Wifi 或蓝牙 [关闭]

在应用程序启动时打开GPS

使用标签、Wifi 和 Gps

高德地图为啥定位不到我现在的位置