【问题标题】:My BroadcastReceiver is called two times when turning on/off wifi or gps?打开/关闭 wifi 或 gps 时,我的 BroadcastReceiver 被调用了两次?
【发布时间】:2017-12-18 16:05:45
【问题描述】:

我在谷歌上搜索了很多次以找到解决方案,但没有奏效。 为这个问题苦苦挣扎了很多天,如果你遇到这个问题并解决了,请帮助我。

当我打开或关闭 wifi 或 Gps 时,接收器会触发两次 虽然有时会触发一次,但如果触发两次它会破坏我的应用程序计划。

提前谢谢...

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

// ......

【问题讨论】:

  • 您需要接收器一直处于活动状态还是仅在应用运行时处于活动状态?
  • 我也需要它在后台处于活动状态。你为什么问@SerjArdovic?
  • 触发两次时,收到的两个intent中的action是什么?两次调用onReceive 之间有多少时间?
  • 每次 gps 或 wifi 为每个(gps/wifi)关闭/打开时,它都会得到正确的操作。并且两次触发之间的时间不到一秒。我通过一个标志和计时器解决了这个问题以将其重新设置,但这不是正确的解决方案。我要找出真正的原因。顺便感谢公司。
  • 我添加了我的解决方案,这可能是暂时的,但现在解决了我的问题。请检查一下 。 @SerjArdovic

标签: java android triggers gps broadcastreceiver


【解决方案1】:

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

【讨论】:

  • 我不认为 JobScheduler 是这里的答案,因为我知道 JobScheduler 可以在 alarmManager 或类似的东西中使用,但不适用于广播接收器,谢谢帮助我的朋友
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多