<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shareitreplica">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" android:required="true" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" android:required="true" />
<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>
Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.shareitreplica"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':library')
}
我的代码
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE)
!= PackageManager.PERMISSION_GRANTED) {
System.out.println("Starting");
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET}, 111);
System.out.println("Starting1");
}
}
我认为这是获得用户许可的最佳方法。官方文档也这样说。但是在运行此代码时,操作系统不会提示我获得许可。
运行代码后,我得到了两个打印行。 我有Android Oreo版本。