lenny
lenny

Reputation: 784

requesting internet permission not working

I'm trying to request internet permission in my android app on my android phone using SDK 26. I've tried using the AndroidManifest.xml, I've tried using ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.INTERNET }, 1); and I've tried different request codes. I've tried this in onCreate and inside a button press event, but still no permissions show up when I go into the App Info.

I'm totally lost as I don't see any reason why this won't work. any Ideas? common problems that I don't know of yet?

andoidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simplevents">

    <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>

Upvotes: 3

Views: 16238

Answers (4)

Vasudev Vyas
Vasudev Vyas

Reputation: 742

You don't need special Permission for Internet.

Just Define Permission in Manifest

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

for testing purpose you can check your internet connection add below permission in Android Manifest file. This Permission give you what is state (Status) of your internet connection.

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

Here Test Code you can using check you internet connection. isNetwork function return true if internet service is working otherwise it returns false.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)


        if (isNetwork(getApplicationContext())){

        Toast.makeText(getApplicationContext(), "Internet Connected", Toast.LENGTH_SHORT).show()

        } else {

        Toast.makeText(getApplicationContext(), "Internet Is Not Connected", Toast.LENGTH_SHORT).show()
        }
    }


    public boolean isNetwork(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

}

Upvotes: 5

lenny
lenny

Reputation: 784

As I'm not allowed to delete this question any more because other people have already put effort into answering the question, I will explain to you what my error was. I did have the permission for internet access the whole time - so I had a completely different fault in my code, and I misinterpreted the error message and thought it was a permission thing, because the internet permission doesn't show up on the app info screen. It never does, because it's not a 'dangerous' permission, that much I've learned.

To summarize: Getting the permission to establish a connection to the internet on android is as easy as adding the permission in the AndroidManifest.xml.

Upvotes: 4

rhaldar
rhaldar

Reputation: 1095

Internet Permission in Normal permission, This permission allow automatically by Android System.

Only Sensitive Permission like CALL_LOG, SMS etc.. are need Run time Permission.

Try this

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

If you are using mobile data , you need only INTERNET permission but if you using Wi-Fi or other network, you need ACCESS_NETWORK_STATE permission,

Note- Network permission do not need on Runtime permission.

Upvotes: 0

Kishore Jethava
Kishore Jethava

Reputation: 6834

Permissions are divided into several protection levels

There are three protection levels that affect third-party apps: normal, signature, and dangerous permissions

INTERNET permission comes in normal protection level and that's why it's not required to ask at runtime

see list here

What is normal and dangerous permission?

Normal :

The permissions which are not sensitive and harmful to users eg. like VIBRATE and INTERNET

Dangerous :

The permissions which are sensitive and harmful like after allowing READ_CONTACTS, The App can misuse your contacts details or allow READ_EXTERNAL_STORAGE can access your SD card data

Upvotes: 6

Related Questions