My BroadcastReceiver is called two times when turning on/off wifi or gps?

大憨熊 提交于 2021-01-28 05:11:54

问题


I have googled many time to find the solution but It didn't work. struggeld with this for many days ,please help me if you faced this problem and solved it.

when I turn on or off wifi or Gps the Receiver triggers two time although sometimes once but if it triggers two time it would corrupt my plans for the app.

thx in advance ...

inside 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>

this the BroadcastReceiver class :

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) {

    }
}

}

***** FOUND A WAY TO SOLVE IT

but I don't know this is the best way to handle it :

I use a flag to check if it is the first time then after 1 or 2 sec return the flag back.

// ....

    @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) {
      
      }

// ......


回答1:


The use of broadcast receiver is deprecated because contribute to low system performance. The proper way to do it is using the JobScheduler.



来源:https://stackoverflow.com/questions/47872186/my-broadcastreceiver-is-called-two-times-when-turning-on-off-wifi-or-gps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!