Android - App WebView - 网络检查

时间:2014-04-30 09:31:01

标签: android

首先,抱歉我的英语不好,我是法国人,要找到易于理解的答案并不容易! 我是Android开发的初学者,我遇到了问题。 我想打印一个网页(它完美无缺)。但是,如果用户没有互联网访问权限,我想打印一个告诉它的通知,以及当用户没有连接时谁不移动。

怎么做? 谢谢!

2 个答案:

答案 0 :(得分:0)

您可以通过此方法检查连接状态。如果返回true,则用户连接到网络

public static boolean checkNetworkState(Context context) {
        ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo infos[] = conMgr.getAllNetworkInfo();
        for (NetworkInfo info : infos) {
            if (info.getState() == State.CONNECTED)
                return true;
        }
        return false;
    }

您还应该将<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />添加到AndroidManifest.xml

答案 1 :(得分:0)

Connectiondetector.java

import android.content.Context;
    import android.net.ConnectivityManager;

    public class ConnectionDetector {

        private Context _context;

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

        public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //return (connectivity != null && connectivity.getActiveNetworkInfo().isConnectedOrConnecting()) ? true : false;
            if (connectivity.getActiveNetworkInfo() != null && connectivity.getActiveNetworkInfo().isAvailable() &&    connectivity.getActiveNetworkInfo().isConnected()) 
            {

                   return true;
             } else {
                   //System.out.println("Internet Connection Not Present");
                 return false;

             }
        }
    }

mainactivity.java

  import java.io.BufferedReader;
    import java.io.IOExceptio;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity
    {
        public EditText uname,pwd;
        Button btnlog1;
        TextView invalid;
        public Button btncancel1;
        public String db_select;
         String mUname;
         String mPwd;
        String temp;
        Intent intObj;
        Intent intent = null;

    Boolean isInternetPresent = false;
        ConnectionDetector cd;


        private final String SERVICE_URL = Urlmanager.Address;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            MainActivity.this.setContentView(R.layout.activity_main);
            uname=(EditText)findViewById(R.id.editText1);
            pwd=(EditText)findViewById(R.id.editText2);
            invalid=(TextView)findViewById(R.id.textView3);
            btnlog1=(Button)findViewById(R.id.button1);
            //btncancel1=(Button)findViewById(R.id.button2);
            //SERVICE_URL=ServerURL.URL+"/msd";

            btnlog1.setOnClickListener(new View.OnClickListener()
            {           
                @Override
                public void onClick(View v) 
                {
                    mUname=uname.getText().toString();
                    mPwd=pwd.getText().toString();

                    if(!mUname.equalsIgnoreCase("") && !mPwd.equalsIgnoreCase(""))
                    {
                        cd = new ConnectionDetector(getApplicationContext());
                        isInternetPresent = cd.isConnectingToInternet();
                        //Toast.makeText(MainActivity.this, isInternetPresent, Toast.LENGTH_LONG).show();
                        if(isInternetPresent)
                        {


                        try
                        {
                            validat_user(mUname,mPwd);

                        }
                        catch(Exception e)
                        {
                            display("Network error.\nPlease check with your network settings.");
                            uname.setText("");
                            pwd.setText("");
                        }
                        }
                        else
                        {
                            display("No Internet Connection...");
                        }

                    }
                    else
                    {
                        invalid.setText("Please enter the data");
                    }
                }
            });

        }
        public void display(String msg) 
        {
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
        }

    }

的manifest.xml

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

进一步查询 use this link