如何使用广播接收器检查互联网连接并更改意图

时间:2018-08-18 23:55:59

标签: java android android-broadcastreceiver

我正在开发一个需要不断侦听Internet连接的Android应用程序。我正在使用广播侦听器并成功应用了它。但是我的代码仅显示Toast消息。

我想停止当前活动并显示一个默认XML文件,其中显示“ No Internet Connection”。并且只要它连接Internet,就会恢复以前的活动。

ExampleBradcastReceiver.java

public class ExampleBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        boolean noConnectivity = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false
        );
        if (noConnectivity) {
            Toast.makeText(context, "Disconnected", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onStart() {
    super.onStart();
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(exampleBroadcastReceiver, filter);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(exampleBroadcastReceiver);
}
}

在Toast Message处,我想在断开连接时显示默认的XML文件,并在连接时恢复活动。

2 个答案:

答案 0 :(得分:1)

您可以将ExampleBroadcastReceiver作为inner class移到MainActivity。并且由于在Java内部类可以访问其父类的方法和字段,因此您可以在onReceive方法中考虑显示/隐藏Internet断开的视图。

public class MainActivity extends AppCompatActivity {
    ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(exampleBroadcastReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(exampleBroadcastReceiver);
    }

    private void showInternetDisconnectedView(boolean disconnected){
        // show or hide based on 'disconnected'
    }

    private class ExampleBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                boolean noConnectivity = intent.getBooleanExtra(
                    ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                showInternetDisconnectedView(noConnectivity);
            }
        }
    }
}

答案 1 :(得分:-1)

您需要将广播接收器代码移到“活动”中,并且在接收Internet连接事件时,可以停止当前正在进行的活动,并使Internet故障布局仅在其中可见,因为它是Activity类的一部分。如果整个应用程序都需要它,则创建Base活动并在那里进行处理,以避免在每个屏幕上重复代码。