本文介绍了为什么我的应用程序无法连接到互联网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个活动为空的新项目,这是我的完整代码:
I created a new project with an empty activity, and this is my full code:
MainActivity.java
MainActivity.java
package com.myapp.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import java.io.IOException; public class MainActivity extends AppCompatActivity{ public boolean isConnectedToInternet(){ Runtime runtime = Runtime.getRuntime(); try{ Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); int exitValue = ipProcess.waitFor(); return (exitValue == 0); }catch (IOException e){ e.printStackTrace(); } catch(InterruptedException e){ e.printStackTrace(); } return false; } @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(isConnectedToInternet()){ Toast.makeText(this, "connected to internet", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "not connected to internet", Toast.LENGTH_SHORT).show(); } } }AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/apk/res/android" package="com.myapp.myapplication"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>当我在模拟器中启动应用程序时,它显示了一个未连接到互联网"的吐司-为什么?
When I start my application in the emulator, it shows a toast "not connected to internet" - why?
并且互联网可以在模拟器上运行,我可以在虚拟设备上的模拟器中使用chrome,并且可以正确显示youtube等. -它仅在应用程序中不起作用.
And the internet works on the emulator, I can use chrome in the emulator on the virtual device and it shows youtube etc correctly. - It only doesn't work in the application.
我想念什么?
推荐答案使用此代码检查intenet连接.
use this code to check intenet connection..in main
TestInternet testInternet = new TestInternet(); testInternet.execute();非主要
class TestInternet extends AsyncTask<Void, Void, Boolean> { @Override protected Boolean doInBackground(Void... params) { try { URL url = new URL("www.google"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setConnectTimeout(4000); urlc.connect(); if (urlc.getResponseCode() == 200) { return true; } } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return false; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return false; } @Override protected void onPostExecute(Boolean result) { if (!result) { // code if not connected else { // code if connected } } }更多推荐
为什么我的应用程序无法连接到互联网?
发布评论