A manifest file prevents me from accessing the internet on Android

0

I have a problem, the following manifest prevents me from accessing the internet in Android .

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.educaapp.toolearning.educaapp">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
        <activity android:name=".Ruleta" />
        <activity android:name=".Preguntas"></activity>
    </application>
</manifest>
    
asked by rafael 10.03.2017 в 05:30
source

3 answers

3

The file AndroidManifest.xml can not restrict access to the internet, it is really to define permissions that will be used by the application, obviously you will not have access to the internet if the permission is not defined:

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

Permission to allow internet connection is not defined within the risky permissions that you must manually request in 6.0+ operating systems, therefore it is sufficient to define the permission within AndroidManifest.xml .

This would be the way to get connectivity:

    ConnectivityManager cm =
 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork.isConnectedOrConnecting();

Your permission statement is correct, the problem may be because you do not have data or access to a WiFi network.

Here the documentation .

    
answered by 10.03.2017 в 17:00
1

If your app makes use of the internet connection, you must declare the following permission in your AndroidManifest.xml file:

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...
</manifest>
    
answered by 10.03.2017 в 05:41
0

As already said, the file manifest.xml implicitly prevents what you do not authorize in it, in case a permission is required. That is, manifest serves to allow, not to prevent. Although from Android 6 there are some permissions considered dangerous, which the manifest can not authorize per se, but it is necessary that the user authorizes them when they are requested, but it is not the case of the internet connection.

In the manifest that shows the internet connection is allowed by this entry:

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

and the one below:

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

has to serve for verify that there is an internet connection available before launching any action that requires its use .

For example:

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork.isConnectedOrConnecting();

If you implement a code like this, before launching anything that has to do with the internet you should ask if the variable isConnected is true.

But ... the one that is connected does not mean that the internet works. Sometimes you are connected to a Wi-Fi but the internet may not be working because your provider's server is down. Or you can be connected to the mobile network, but have no credit in your plan to use data transmission ... You need then a supplementary check that includes these cases. This could be the subject of an interesting question in esSO :)

    
answered by 19.03.2017 в 01:58