BroadcastReceiver仅第二次触发

时间:2017-12-09 07:10:01

标签: android broadcastreceiver

我正在通过ping到google来检查Internet连接。如果没有网络或响应不成功,我发送一条广播,其中包含没有互联网的消息。每当我点击导航抽屉中的Textview时,它必须检查是否有互联网连接。当没有互联网并且我点击文本视图时它没有被选中,它只在我第二次点击Textview时被检查.BroadcastReceiver的onReceive方法不是第一次打电话。

以下是代码:

public void checkInternetConnection()
    {
        connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo=connectivityManager.getActiveNetworkInfo();
        try {
            if (networkInfo!=null)
            {
                if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE || networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
                {
                    OkHttpClient okHttpClient = new OkHttpClient.Builder().
                            connectTimeout(30, TimeUnit.SECONDS).
                            writeTimeout(30, TimeUnit.SECONDS).
                            readTimeout(30, TimeUnit.SECONDS).build();
                    Request request = new Request.Builder().url(googleurl).build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e("error", "error");
                            e.printStackTrace();
                            hasInternet1 = false;
                            Intent intent=new Intent("itrnt_avl");
                            intent.putExtra("inernet_availability",false);
                            sendBroadcast(intent);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {

                            Log.e("Response", response.body().string());
                            if (response.isSuccessful()) {
                                hasInternet1 = true;
                                Intent intent=new Intent("itrnt_avl");
                                intent.putExtra("inernet_availability",true);
                                sendBroadcast(intent);


                            } else {
                                Log.e("Response", "No response");
                                hasInternet1 = false;
                                Intent intent=new Intent("itrnt_avl");
                                intent.putExtra("inernet_availability",false);
                                sendBroadcast(intent);
                                throw new IOException("Unexpected code " + response.body().string());
                            }
                        }
                    });
                }
                else
                {
                    hasInternet1 = false;
                    Log.e("Response", "No network type");
                    Intent intent=new Intent("itrnt_avl");
                    intent.putExtra("inernet_availability",false);
                    sendBroadcast(intent);
                }
            }
            else {
                hasInternet1=false;
                Log.e("Response", "network null");
                Intent intent=new Intent("itrnt_avl");
                intent.putExtra("inernet_availability",false);
                sendBroadcast(intent);
            }
        }
        catch (NullPointerException e)
        {
            e.printStackTrace();
            Intent intent=new Intent("itrnt_avl");
            intent.putExtra("inernet_availability",false);
            sendBroadcast(intent);
        }
    }

 bad_dates.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    drawerlayout.closeDrawers();
     checkInternetConnection();
     if (broadcastReceiver==null)
     {
     Log.e("Receiver","Not null");

     broadcastReceiver=new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
     Log.e("OnReceive","Called");

     hasInternet1=intent.getBooleanExtra("inernet_availability",false);
     unregisterReceiver(broadcastReceiver);
     broadcastReceiver=null;
     if (hasInternet1){
     new BackgroundTask1().execute();
     }
     else {
     Log.e("Internet Status","No Internet");
     Toast.makeText(getApplicationContext(),"Please check your 
     InternetConnection and try again later",                       
     Toast.LENGTH_SHORT).show();
                                    }
                                }
                            };
     registerReceiver(broadcasteceiver,new IntentFilter("itrnt_avl"));
                        }
      });

1 个答案:

答案 0 :(得分:3)

这里的问题是你首先调用发布广播然后注册的checkInternetConnection()。这意味着,第一次没有人接收广播。

当您第二次运行checkInternetConnection()时,您已经第一次注册了接收器,因此它接收了广播。

因此,您必须先注册广播接收器,然后调用方法checkInternetConnection()

此外,正如您在评论中所述,broadcastReceiver是已收到的注册。方法onReceive()仅在收到抵制广播时才会执行。