android studio版本:3.4.1
build.gradle: 必须按照以下版本写,否则api25以上无法获取权限
apply plugin: 'com.android.application'
android {
compileSdkVersion 25 //这里提示要26以上,会红色报错,不管他
defaultConfig {
applicationId "com.example.evan.test"
minSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.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 files('libs/commons-io-2.6.jar')
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.android.support:support-annotations:28.0.0'
}
androidmanifest
roundIcon
main
String macString=""; //mac地址
private static String[]PERMISSIONS_STORAGE={ //需要的權限數組
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
Manifest.permission.INTERNET,
Manifest.permission.INSTALL_PACKAGES,
};
//请求状态码
private static int REQUEST_PERMISSION_CODE = 1;
private TextView textView;
// private String sdcardPath; //内存路径
Reader reader = null;
File file=null;
String tvShowString="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_show);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
//单独获取权限
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) { //判断是否android6.0以上
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
for(int i=0;i
输出:
2013-01-18 18:28:27.075 4687-4687/? I/wp----->: 执行完android.permission.WRITE_EXTERNAL_STORAGE权限为--》0
2013-01-18 18:28:27.076 4687-4687/? I/wp----->: 执行完android.permission.READ_EXTERNAL_STORAGE权限为--》0
2013-01-18 18:28:27.076 4687-4687/? I/wp----->: 执行完android.permission.MOUNT_UNMOUNT_FILESYSTEMS权限为--》-1
2013-01-18 18:28:27.077 4687-4687/? I/wp----->: 执行完android.permission.INTERNET权限为--》0
2013-01-18 18:28:27.077 4687-4687/? I/wp----->: 执行完android.permission.INSTALL_PACKAGES权限为--》-1