【问题标题】:How to check internet connectivity using broadcast receiver and Change the intent如何使用广播接收器检查互联网连接并更改意图
【发布时间】:2018-08-19 12:45:15
【问题描述】:

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

我想停止当前活动并显示一个默认的 XML 文件,上面写着“没有 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 消息的位置,我想在断开连接时显示默认 XML 文件并在连接时恢复活动。

【问题讨论】:

标签: java android android-broadcastreceiver


【解决方案1】:

您可以将ExampleBroadcastReceiver 移动到MainActivity 作为inner class。由于在 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);
            }
        }
    }
}

【讨论】:

  • 我认为在应用程序对象上也有一个可能会更好,但考虑到当你启动应用程序时它会立即进入“MainActivity”,最好在活动生命周期中使用这个逻辑而不是应用程序的。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多