简体   繁体   English

请求互联网许可不起作用

[英]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);我正在尝试使用 SDK 26 在我的 android 手机上的 android 应用程序中请求互联网权限。我尝试使用AndroidManifest.xml ,我尝试使用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.我已经在onCreate和按钮按下事件中尝试过这个,但是当我进入应用信息时仍然没有显示权限。

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

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 INTERNET权限处于正常保护级别,这就是为什么不需要在运行时询问的原因

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在允许READ_CONTACTS之后敏感和有害的权限,该应用程序可能会滥用您的联系人详细信息或允许READ_EXTERNAL_STORAGE可以访问您的 SD 卡数据

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.出于测试目的,您可以检查您的互联网连接在 Android 清单文件中添加以下权限。 This Permission give you what is state (Status) of your internet connection.此权限为您提供 Internet 连接的状态(状态)。

<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.如果 Internet 服务正在工作,isNetwork 函数返回 true,否则返回 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;
    }

}

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 .总结:获得在 android 上建立互联网连接的权限就像在AndroidManifest.xml添加权限一样简单。

Internet Permission in Normal permission, This permission allow automatically by Android System.普通权限中的Internet权限,此权限由 Android 系统自动允许。

Only Sensitive Permission like CALL_LOG, SMS etc.. are need Run time Permission.只有 CALL_LOG、SMS 等敏感权限才需要运行时权限。

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,如果您使用的是移动数据,则只需要 INTERNET 权限,但如果您使用 Wi-Fi 或其他网络,则需要 ACCESS_NETWORK_STATE 权限,

Note- Network permission do not need on Runtime permission.注意 - 网络权限不需要运行时权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM