gpt4 book ai didi

java - 在互联网连接打开时打开 Android 应用程序,否则显示无互联网连接消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:48 25 4
gpt4 key购买 nike

我刚刚创建了一个用于从网站获取数据的 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();
}
}
}

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

最佳答案

试试这个

//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;
}

一旦方法返回了你必须检查的值

//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();

}

关于java - 在互联网连接打开时打开 Android 应用程序,否则显示无互联网连接消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com