![](/img/trans.png)
[英]I can't get files that are outside my app folder Android studio
[英]I can't get internet on my android app
我只有我的android应用,但无法获得互联网连接
这是我的AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.onscreenapp.app" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<activity
android:name="com.example.onscreenapp.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
如您所见,我对此具有权限。
我尝试使用2个功能检查互联网状态
一个是这个:
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
if (ipAddr.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
返回false,另一个是这个:
public final boolean isInternetOn() {
// get Connectivity Manager object to check connection
ConnectivityManager connec =
(ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
// Check for network connections
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
// if connected with internet
Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
return true;
} else if (
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
return false;
}
return false;
}
我正在android S4 mini上通过设备测试此应用程序,当我编译并启动它时,我得到“ appname has stop”,显示为运行时错误。
放在清单的下面,不在应用程序中
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stackoverflowandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.onscreenapp.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
----------
public static boolean isInternetAvailable(Context c) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) c
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
使用上述清单和连接可用的代码
isInternetAvailable()方法返回false。 因此,这意味着尽管您已连接到网络,但是由于以下代码行而无法访问Internet:
InetAddress ipAddr = InetAddress.getByName("google.com");
基本上是对Google的回应。
请发布日志以获取更好的帮助。
PS-您也可以在这里参考@Squonk的答案: 检测Android上是否存在互联网连接
对不起,我迟来了答复,但正在查找此问题,原因是我的应用在4.4 android平板电脑上运行时遇到了同样的问题,在我的日志猫上它显示出查询互联网连接方法的代码有问题。 所以我删除了以下几行,在其中我用双斜杠将它们注释掉了,现在我的应用程序可以正常工作了。 也许在这种情况下,您必须做同样的事情。 希望它对您有用,对我也一样。 下面是代码...
//A method that checks to see if the Users phone is connected to the internet or not
public boolean isConnetedToInternetOrNot() {
// get Connectivity Manager object to check connection
connec = (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
if (
//connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED //||
// connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING //||
// connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED
) {
return true;
} else {
return false;
}
}
将此代码放在您的AndroidManifest xml文件中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.