检查互联网状态后更改ImageView图像

时间:2012-10-31 07:04:12

标签: android sdk

我想在检查互联网雕像后更改图像视图图像。 基本的,我想检查互联网是否有效,如果我想要显示某个图像。

这里是我的代码:

public void onClick(View v) {

    // get Internet status
    isInternetPresent = cd.isConnectingToInternet();

    // check for Internet status
    if (isInternetPresent) {
        // Internet Connection is Present
        // make HTTP requests
        showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection", "You have internet connection", true);
    } else {
        // Internet connection is not present
        // Ask user to connect to Internet
        showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection", "You don't have internet connection.", false);
    }
}

3 个答案:

答案 0 :(得分:0)

我假设你知道在Android上检查互联网。

在oncreate中

使用:

imagev = (ImageView)findviewbyid(R.id.imageview); 

onbutton click:

public void onClick(View v) {

                // get Internet status
                isInternetPresent = cd.isConnectingToInternet();

                // check for Internet status
                if (isInternetPresent) {
                    // Internet Connection is Present
                    // make HTTP requests
                    showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                            "You have internet connection", true);
                              imagev.setImageDrawable(getResources().getDrawable(R.drawable.withinternet));

                } else {
                    // Internet connection is not present
                    // Ask user to connect to Internet
                    showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                            "You don't have internet connection.", false);
                 imagev.setImageDrawable(getResources().getDrawable(R.drawable.nointernet));
                }

答案 1 :(得分:0)

使用以下方法检查互联网是否可用

public static boolean isOnline(Activity context) {
    ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    }
    return false;
}

在清单文件中设置以下权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

然后检查如下

if(isOnline())
    img.setImageResource();
else
    img.setImageResource();

答案 2 :(得分:0)

尝试更改ImageView的图像。

 your_ImageView.setBackgroundResource(R.drawable.your_image_name);