如何在Webview之前检查Internet权限在android Project中加载URL [英] How to check the Internet Permission before the Webview Loading the URL in android Project

查看:87
本文介绍了如何在Webview之前检查Internet权限在android Project中加载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个应用程序,其中我使用了Web视图。但是当没有互联网连接时,应用程序会显示找不到网址的消息或找不到网页错误消息。我想要的是,如果设备中没有互联网连接,则显示Toast消息(请检查您的互联网连接 n)而不是加载URL。请帮忙解决这个问题。

我的代码如下: -

I have made an application in which i have used web view. but when there is no internet connection the application shows the url not found message or web page not found error message. what i want is that if there is no internet connection in the device then a toast message("Please check your internet connection") is displayed instead of loading URL. Please help in resolving this problem.
My code is below:-

package com.iapits.jpsc;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.servesh.jpsc.R;

/**
 * Created by Servesh on 10-01-2016.
 */
public class WebViewMethod extends Activity {

    private WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        view = (WebView) findViewById(R.id.webView1);
        view.setWebViewClient(new MyWebViewClient());
        String url = "http://android-mocktest-portal.indianstudyspot.in/issjpscquiz";

        view.getSettings().setJavaScriptEnabled(true);
       view.loadUrl(url);
        ActionBar actionBar = getActionBar();
        //ActionBar actionBar = getSupportActionBar();
        actionBar.setLogo(R.drawable.logo);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}







---- ---------------



MainActivity.java





案例R.id.bt12:










-------------------

MainActivity.java


case R.id.bt12:



@Override
   public void onClick(View v){
       switch(v.getId()) {



// Intent browserIntent = new Intent(android.intent.action.VIEW,Uri.parse(http://android-mocktest-portal.indianstudyspot.in/)) ;

startActivity(new Intent(this,WebViewMethod.class));

break;

}


// Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://android-mocktest-portal.indianstudyspot.in/"));
startActivity(new Intent(this, WebViewMethod.class));
break;
}

推荐答案

要检查您的设备是否已连接到互联网,请使用:

To check if your device is internet connected or not use:
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}



在Manifest.xml中使用权限:


Use a permission in your Manifest.xml:

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



然后使用如下方法:


And then use the method like:

if (!isNetworkAvailable) {
    // write your toast message("Please check your internet connection")
} else {
    // code
}


参见 Android检测互联网连接状态教程 [ ^ ]。


这篇关于如何在Webview之前检查Internet权限在android Project中加载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆