打开互联网连接时打开Android应用程序,否则显示没有互联网连接消息

pra*_*hik 8 java connection android android-listview

我刚创建了一个用于从网站获取数据的Android应用程序.我想检查设备是否有互联网连接.如果设备具有互联网连接,请运行我的代码并获取数据并显示它,否则如果设备没有互联网,则显示无互联网连接消息.我试过这段代码来检查互联网连接.如果有互联网连接,我该如何调用代码?

我的Java代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary);
        new FetchWebsiteData().execute();        
            }
        });

    }

    private class FetchWebsiteData extends AsyncTask<Void, Void, String[]> {
        String websiteTitle, websiteDescription,websiteDescription1,websiteDescription2,websiteDescription3,listValue,listValue1;
        ProgressDialog progress;
        private Context context;

        //check Internet connection.
        private void checkInternetConnection(){

            ConnectivityManager check = (ConnectivityManager) this.context.
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            if (check != null)
            {
                NetworkInfo[] info = check.getAllNetworkInfo();
                if (info != null)
                    for (int i = 0; i <info.length; i++)
                        if (info[i].getState() == NetworkInfo.State.CONNECTED)
                        {
                            Toast.makeText(context, "Internet is connected",
                                    Toast.LENGTH_SHORT).show();

                        }

            }
            else{
                Toast.makeText(context, "not conencted to internet",
                        Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //some code here
        }

        @Override
        protected String[] doInBackground(Void... params) {
            ArrayList<String> hrefs=new ArrayList<String>();
            try {

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            //get the array list values
            for(String s:hrefs)
            {
                //some code
            }
            //parsing first URL
            String [] resultArray=null;
            try {


            } catch (IOException e) {
                e.printStackTrace();
            }
            //parsing second URL
            String [] resultArray1=null;
            try {



            } catch (IOException e) {
                e.printStackTrace();
            }

            try{


            } catch (Exception e) {
                e.printStackTrace();
            }


            return null;
        }



        @Override
        protected void onPostExecute(String[] result) {

            ListView list=(ListView)findViewById(R.id.listShow);
            ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,result);
            list.setAdapter(arrayAdapter);
            mProgressDialog.dismiss();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在连接打开时运行代码以及如何在应用程序没有Internet连接时显示消息?

小智 8

试试这个

//check internet connection
public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null)
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
        {
            return netInfos.isConnected();
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

一旦该方法返回您必须检查的值

//detect internet and show the data
    if(isNetworkStatusAvialable (getApplicationContext())) {
        Toast.makeText(getApplicationContext(), "Internet detected", Toast.LENGTH_SHORT).show();
        new FetchWebsiteData().execute();
    } else {
        Toast.makeText(getApplicationContext(), "Please check your Internet Connection", Toast.LENGTH_SHORT).show();

    }
Run Code Online (Sandbox Code Playgroud)