View Full Version : سوال : چک کردن دسترسی به اینترنت
perkas
سه شنبه 08 بهمن 1392, 22:35 عصر
سلام
من یه webview تو برنامم دارم ولی میخوام قبلش برنامه چک کنه که اگه دسترسی به اینترنت نبود متن " دسترسی به شبکه مقدور نمیباشد " به نمایش در بیاد در غیر این صورت webview به نمایش در بیاد و لینک مربوطه رو نمایش بده
saeed_g21
سه شنبه 08 بهمن 1392, 23:19 عصر
public class internet extends Activity
{
ImageView image;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.internet);
boolean status = checkInternetConnection();
if (status){
image=(ImageView)findViewById(R.id.net_img);
text= (TextView)findViewById(R.id.net_txt);
text.setText("دستگاه به سرویس اینترنت متصل می باشد");
image.setImageResource(R.drawable.yes);
}else{
image=(ImageView)findViewById(R.id.net_img);
image.setImageResource(R.drawable.no);
text= (TextView)findViewById(R.id.net_txt);
text.setText("دستگاه فاقد سرویس اینترنت می باشد");
image.setImageResource(R.drawable.no);
}
}
public boolean checkInternetConnection()
{
ConnectivityManager connectivity = (ConnectivityManager)getApplicationContext().getSy stemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null){
NetworkInfo[] inf = connectivity.getAllNetworkInfo();
if (inf != null)
for (int i = 0; i < inf.length; i++)
if (inf[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.