我有以下代码在活动的READ_EXT_STORAGE
中请求onCreate()
权限:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setMessage("This app needs to read your External Storage/Gallery to provide the images and the music you're going to use in the video.").setPositiveButton("OK", null).create();
adb.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else
{
onCreateActual();
}
}
onCreateActual()
包含要在onCreate()
中执行的实际代码。它包括动态设置片段。
onStartActual()
涉及阅读外部存储空间以获取相册名称,并将它们放入片段中的ListView中。
这就是我的onRequestPermissionsResult()
:
private int MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE = 102;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.i("perms", permissions.length+"");
int indexOfReadStorage = 0;
for(int i = 0 ; i < permissions.length; i++)
{
Log.i("perms_len", permissions[i]);
if(permissions[i].contains("READ_EXTERNAL_STORAGE"))
{
indexOfReadStorage = i;
}
}
if (grantResults.length > 0 && grantResults[indexOfReadStorage] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Log.i("read_ext_storage_perm", "granted");
onCreateActual();
} else {
// Permission Denied
Toast.makeText(this, "READ_EXT_STORAGE Denied", Toast.LENGTH_SHORT)
.show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
我的问题是我有一个要求READ_EXTERNAL_STORAGE权限的广告库,如果我在此之前而不是在else子句中放置onCreateActual()
,则库首先要求该权限,{{1上面不会被调用。
但是,如果我这样做,onRequestPermissionsResult()
让我知道:
java.lang.IllegalStateException:onSaveInstanceState之后无法执行此操作
所以,除非有更好的方法,否则我认为我必须以某种方式停止活动的生命周期,直到授予READ_EXTERNAL_STORAGE权限。
是否可以延迟活动的生命周期,直到变量变化为止?如果没有,我可以刷新片段吗?
答案 0 :(得分:0)
我认为你应该尝试Ask它运作良好并取出大部分的hastle
只需编译依赖
compile 'com.vistrav:ask:2.1'
这是完整的例子
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ask.on(this)
.forPermissions(Manifest.permission.ACCESS_COARSE_LOCATION
, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withRationales("Location permission need for map to work properly",
"In order to save file you will need to grant storage permission") //optional
.go();
}
//optional
@AskGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void fileAccessGranted() {
Log.i(TAG, "FILE GRANTED");
}
//optional
@AskDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void fileAccessDenied() {
Log.i(TAG, "FILE DENiED");
}
//optional
@AskGranted(Manifest.permission.ACCESS_COARSE_LOCATION)
public void mapAccessGranted() {
Log.i(TAG, "MAP GRANTED");
}
//optional
@AskDenied(Manifest.permission.ACCESS_COARSE_LOCATION)
public void mapAccessDenied() {
Log.i(TAG, "MAP DENIED");
}
}
您可以找到更多详情here
希望有所帮助