![](/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.