[英]What permission do I need to access Internet from an Android application?
I get the following Exception running my app:运行我的应用程序时出现以下异常:
java.net.SocketException: Permission denied (maybe missing INTERNET permission)
How do I solve the missing permission problem?如何解决缺少权限的问题?
In the latest release of Google Play, Google removed the need to ask permission for internet as "most apps need it anyways nowadays".在最新版本的 Google Play 中,Google 取消了请求互联网许可的需要,因为“现在大多数应用程序都需要它”。 However, for users who have older versions, it is still recommended to leave the code below in your manifest
但是,对于旧版本的用户,仍然建议将下面的代码保留在您的清单中
<uses-permission android:name="android.permission.INTERNET" />
just put above line like below就像下面一样放在上面
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.avocats.activeavocats"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.exp.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>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
If you want using Internet in your app as well as check the network state ie Is app is connected to the internet then you have to use below code outside of the application
tag.如果您想在您的应用程序中使用互联网并检查网络状态,即应用程序是否已连接到互联网,那么您必须在
application
标签之外使用以下代码。
For Internet Permission:互联网许可:
<uses-permission android:name="android.permission.INTERNET" />
For Access network state:对于接入网络状态:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Complete Code:完整代码:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<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=".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>
if just using internet then use-如果只是使用互联网然后使用-
<uses-permission android:name="android.permission.INTERNET" />
if you are getting the state of internet then use also -如果您正在获得互联网状态,那么也可以使用 -
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
just above the application tag.就在应用程序标签上方。
Use these:使用这些:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
I had the same problem even use <uses-permission android:name="android.permission.INTERNET" />
我什至有同样的问题使用
<uses-permission android:name="android.permission.INTERNET" />
If you want connect web api using http
not https
.如果您想使用
http
而不是https
连接 web api。 Maybe you use android device using Android 9 (Pie) or API level 28 or higher .也许您使用的是 Android 9 (Pie) 或 API 级别 28 或更高版本的 android 设备。
android:usesCleartextTraffic
default value is false
. android:usesCleartextTraffic
默认值为false
。 You have to set be你必须设置为
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true" <!-- this line -->
...>
...
</application>
</manifest>
Finally, should be https
最后应该是
https
https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic
forget about adding the permission into the manifest Add this code as a method忘记将权限添加到清单中 将此代码添加为方法
public static boolean hasPermissions(Context context, String... permissions)
{
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
{
for (String permission : permissions)
{
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
{
return false;
}
}
}
return true;
}
and write this in your Main并将其写入您的Main
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Just Add these 2 permissions只需添加这两个权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in your app's AndroidManifest.xml
在您应用的
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.networkusage"
...>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
...>
...
</application>
</manifest>
Happy Coding:)快乐编码:)
Just put below code in AndroidManifest :只需将以下代码放在 AndroidManifest 中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
As per current versions, Android doesn't ask for permission to interact with the internet but you can add the below code which will help for users using older versions Just add these in AndroidManifest根据当前版本,Android 不要求与互联网交互的许可,但您可以添加以下代码,这将有助于使用旧版本的用户只需在 AndroidManifest 中添加这些代码
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
To request for internet permission in your code you must add these to your AndroidManifest.xml file要在您的代码中请求互联网权限,您必须将这些添加到您的 AndroidManifest.xml 文件中
<uses-permission android:name="android.permission.INTERNET" />
For more detail explanation goto https://developer.android.com/training/basics/network-ops/connecting有关更多详细说明,请转到https://developer.android.com/training/basics/network-ops/connecting
Google removed the need to ask permission for the internet for the latest version.谷歌取消了为最新版本请求互联网许可的需要。 Still, to request for internet permission in your code you must add these to your AndroidManifest.xml file.
尽管如此,要在您的代码中请求互联网权限,您必须将这些添加到您的 AndroidManifest.xml 文件中。
<uses-permission android:name="android.permission.INTERNET"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.