在android studio应用程序中显示默认屏幕图像

时间:2016-08-12 10:18:43

标签: java android android-studio

对于Android开发的新手,面临的问题是我有一个Web视图应用程序,我想在屏幕上显示图像,显示互联网未连接。我已经申请了互联网连接检查。但现在我不知道如何添加或添加什么代码以在该检查中显示图像。以下代码我用于Check。

ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = cManager.getActiveNetworkInfo();
    if(nInfo !=null && nInfo.isConnected())
    {
       // Toast.makeText(this, "", Toast.LENGTH_LONG).show();
    }
    else
    {


       Toast.makeText(this, "No Internet Connection", Toast.LENGTH_LONG).show();

        for (int i=0; i < 10; i++)
        {
            Toast.makeText(this, "Network is Not available", Toast.LENGTH_LONG).show();
        }
        //Toast.makeText(this, "Network is Not available", Toast.LENGTH_LONG).show()


    }

1 个答案:

答案 0 :(得分:0)

使用此功能;

这样可以正常使用。

 connection = (ImageView) findViewById(R.id.image_view);
        note = (TextView) findViewById(R.id.err);
        cd = new ConnectionDetector(getApplicationContext());
        // get Internet status
        isInternetPresent = cd.isConnectingToInternet();

    if (!isInternetPresent) {
        hidePDialog();
        connection.setVisibility(View.VISIBLE);
        note.setVisibility(View.VISIBLE);
    } else {
             connection.setVisibility(View.INVISIBLE);
            note.setVisibility(View.INVISIBLE);

             **Do What U want here**

}

ConnectDetector类

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();

        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }

    }
    return false;
}
}