0

我正在努力检查互联网连接的能力,就像 YouTube 目前在 Android 应用上所做的那样。

问题是如果互联网可用,它每次都显示为在线。我想让它就像它已经在线一样,不显示任何消息,但是当不在线时,每次都显示它。

这是我的 MainActivity 代码:

public class LoginActivity extends AppCompatActivity implements NetworkReceiver.ConnectionChangeCallback{

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

        IntentFilter intentFilter02 = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        networkReceiver = new NetworkReceiver();
        registerReceiver(networkReceiver, intentFilter02);
        networkReceiver.setConnectionChangeCallback(this);
        }

    @Override
    public void onConnectionChange(boolean isConnected) {

        if (isConnected){
            // will be called when internet is gone.
            tv_check_connection.setText("Back Online");
            connection.setBackgroundColor(Color.parseColor("#00BE84"));
            connection.startAnimation(slideDownToUp);
            tv_check_connection.setTextColor(Color.WHITE);

            Handler handler = new Handler();
            Runnable delayrunnable = new Runnable() {
                @Override
                public void run() {
                    tv_check_connection.setVisibility(View.GONE);
                }
            };
            handler.postDelayed(delayrunnable, 4000);

        }else{
            // will be called when internet is back
            new MyDialog().show(getSupportFragmentManager(), "my_dialog");
            tv_check_connection.setText("No connection");
            tv_check_connection.setVisibility(View.VISIBLE);
            connection.startAnimation(slideDownToUp);
            connection.setBackgroundColor(Color.parseColor("#2D2D2D"));
            tv_check_connection.setTextColor(Color.WHITE);
        }

    }
}

这是我的 NetworkReceiver 类:

public class NetworkReceiver extends BroadcastReceiver {

    public static final String NETWORK_AVAILABLE_ACTION = "com.example.ritecare.NetworkAvailable";
    public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";

    ConnectionChangeCallback connectionChangeCallback;

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
        networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isOnline(context));
        LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);

        if (connectionChangeCallback != null) {
            connectionChangeCallback.onConnectionChange(isOnline(context));
        }
    }


    private boolean isOnline(Context context) {

        try {

            if (context != null) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                //boolean isConnected = networkInfo != null && networkInfo.isConnectedOrConnecting();
                return networkInfo != null && networkInfo.isConnected();
            }
            return false;

        } catch (Exception e) {
            Log.e(NetworkReceiver.class.getName(), e.getMessage());
            return false;
        }
    }

    public void setConnectionChangeCallback(ConnectionChangeCallback connectionChangeCallback) {
        this.connectionChangeCallback = connectionChangeCallback;
    }

    public interface ConnectionChangeCallback {

        void onConnectionChange(boolean isConnected);

    }

}

如何做到这一点?

4

1 回答 1

0

删除static表单视图。将视图作为参数传入 method dialog03

检查此代码。

 TextView tv_check_connection; 
 LinearLayout connection;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    tv_check_connection = (TextView) findViewById(R.id.tv_check_connection_opdipd);
    connection = (LinearLayout) findViewById(R.id.check_connection_opdipd);

// for calling dialog03 method
// dialog03(true, tv_check_connection, connection );
}

public static void dialog03(boolean value, TextView tvCheckConnection, LinearLayout connectionLayout) {


    if (value) {
        tvCheckConnection.setText("Back online");
        connectionLayout.setBackgroundColor(Color.parseColor("#00BE84"));
        connectionLayout.startAnimation(slideDownToUp);
        tvCheckConnection.setTextColor(Color.WHITE);

        Handler handler = new Handler();
        Runnable delayrunnable = new Runnable() {
            @Override
            public void run() {
                if(tvCheckConnection != null){
                   tvCheckConnection.setVisibility(View.GONE);
                }
            }
        };
        handler.postDelayed(delayrunnable, 4000);
    } else {
        tvCheckConnection.setVisibility(View.VISIBLE);
        tvCheckConnection.setText("No connection");
        connectionLayout.startAnimation(slideDownToUp);
        connectionLayout.setBackgroundColor(Color.parseColor("#2D2D2D"));
        tvCheckConnection.setTextColor(Color.WHITE);

    }
}

当您在延迟一段时间后更新视图的可见性时,其安全方式检查该视图是否为空。

于 2019-09-30T05:40:06.607 回答