我正在尝试为MarshMallow +设备设置运行时权限。 当我这样做时请求许可但是当我点击允许应用程序停止不幸。 如果我再次打开应用程序,它工作正常。 我的运行时权限代码:
int CurrentVersion = Build.VERSION.SDK_INT;
if (CurrentVersion >= android.os.Build.VERSION_CODES.M) {
if (!checkIfAlreadyhavePermission())
{
requestForSpecificPermission();
}
}
}
private boolean checkIfAlreadyhavePermission() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestForSpecificPermission() {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == 1) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
createall();
} else {
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
}
createall:
public void createall()
{
String mediaLocation = "/VApp";
String myfolder= Environment.getExternalStorageDirectory() + mediaLocation;
File mediaDir = new File(myfolder);
if(!mediaDir.exists())
{
mediaDir.mkdir();
}
Toolbar toolBar =(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RecentActivity(), "Recent");
adapter.addFragment(new SavedActivity(), "Saved");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
这是我的createall方法但是当我在应用程序停止后再次打开应用程序时,此代码正常工作。
答案 0 :(得分:0)
这是因为:
Toolbar toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RecentActivity(), "Recent");
adapter.addFragment(new SavedActivity(), "Saved");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
上面的代码是用onCreate()
方法
只需将其剪切并粘贴到onPostResume()
方法中即可
像这样:
@Override
protected void onPostResume() {
super.onPostResume();
Toolbar toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RecentActivity(), "Recent");
adapter.addFragment(new SavedActivity(), "Saved");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}