1

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

// ......

Community
  • 1
  • 1
iDeveloper
  • 1,699
  • 22
  • 47
  • Do you need your receiver to be active all the time or only while your app is running? – Sergey Emeliyanov Dec 18 '17 at 16:07
  • 1
    I need it to be active in the background too. why did you ask that @SerjArdovic? – iDeveloper Dec 18 '17 at 16:11
  • 2
    When it triggers twice, what are the actions in the two intents received? And how much time is there between the two calls to `onReceive`? – clownba0t Dec 18 '17 at 16:46
  • 1
    it gets the proper action each time gps or wifi turns off/on for each one(gps/wifi). and the time between two triggers is less than a second . I solved the problem by a flag and timer to set it back but that is not the proper solution. I want to find the real reason. by the way thx for corporation. – iDeveloper Dec 18 '17 at 17:00
  • 1
    I added my solution, this might be temporary but solved my problem right now. please check it . @SerjArdovic – iDeveloper Dec 19 '17 at 05:15

1 Answers1

0

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

exe
  • 107
  • 6
  • 1
    I don't think that the JobScheduler is the answer here ,as I understand JobScheduler can be used in alarmManager or something like that but not for the BroadCast reciever , thx for helping my friend – iDeveloper Dec 19 '17 at 05:18