flutter compat added android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE解决实例
时间: 2023-11-17 21:24:28 浏览: 210
为了在Flutter应用中使用WRITE_EXTERNAL_STORAGE和READ_PHONE_STATE权限,需要在AndroidManifest.xml文件中添加以下代码:
```
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
```
然后,在您的Flutter项目中的android/app/build.gradle文件中添加以下依赖项:
```
dependencies {
implementation 'com.android.support:support-compat:28.0.0'
}
```
最后,在您的Flutter项目中的MainActivity.java文件中添加以下代码:
```
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 2);
}
}
}
```
这个代码片段将检查WRITE_EXTERNAL_STORAGE和READ_PHONE_STATE权限是否被授予,并提供请求这些权限的选项。当用户授权这些权限后,您的应用程序就可以使用这些权限了。
阅读全文