What permission do I need to access Internet from an Android application?

前端 未结 13 1679
名媛妹妹
名媛妹妹 2020-11-22 03:50

I get the following Exception running my app:

java.net.SocketException: Permission denied (maybe missing INTERNET permission)

How do I solv

相关标签:
13条回答
  • 2020-11-22 04:30

    Add the INTERNET permission to your manifest file.

    You have to add this line:

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    outside the application tag in your AndroidManifest.xml

    0 讨论(0)
  • 2020-11-22 04:31

    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

    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);
        }
    
    0 讨论(0)
  • 2020-11-22 04:34
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    0 讨论(0)
  • 2020-11-22 04:35

    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.

    0 讨论(0)
  • 2020-11-22 04:36

    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" />
    
    0 讨论(0)
  • 2020-11-22 04:36

    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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    0 讨论(0)
提交回复
热议问题