打开/关闭wifi或gps时,我的BroadcastReceiver被调用了两次? [英] My BroadcastReceiver is called two times when turning on/off wifi or gps?

查看:111
本文介绍了打开/关闭wifi或gps时,我的BroadcastReceiver被调用了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Google搜索了很多时间来找到解决方案,但这没有用.挣扎了很多天,如果您遇到此问题并解决了,请帮助我.

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.

当我打开或关闭wifi或Gps时,接收器会触发两次尽管有时会触发一次,但是如果触发两次,则会破坏我对应用程序的计划.

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.

提前谢谢...

内部manifest.xml中:

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>

这是BroadcastReceiver类:

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 :

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

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

//......

推荐答案

不建议使用广播接收器,因为这会导致系统性能下降.正确的方法是使用

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

这篇关于打开/关闭wifi或gps时,我的BroadcastReceiver被调用了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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