0

Recently i work to develope an Android which do search and download files... The app works fine in old versions of Android and but it was crashing in version 6 and on... so i add a new permission for file storage. Now when i open the app and want to search something , it gives me a notification about accessing media and storage and asks me to allow or deny..i accept it but after search it asks again and when i press download button, it ask again ... Would it be possible if the app asks only once or maybe when a new user install it for the first time, it asks them and then not again and again?

This is part of the code which i think maybe helpful for you to see me improve this. Thanks

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence titles[] = {"Search Results"};
CharSequence titles2[] = {""};
int numbOfTabs = titles.length;
int numbOfTabs2 = titles2.length;
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);





    // Creating The Toolbar and setting it as the Toolbar for the activity
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);


    Intent intent = getIntent();

    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles, numbOfTabs, false);
    else
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles2, numbOfTabs2, true);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assigning the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabs_scroll_color);


        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        assert getSupportActionBar() != null;
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        String query = intent.getStringExtra(SearchManager.QUERY);
        this.setTitle(query);
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

'

  • Possible duplicate of [Android marshmallow request permission?](http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) – AxelH Dec 01 '16 at 13:48

1 Answers1

1

You should just check if the permission as already been granted before asking for it:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);                     
}
Damien R.
  • 3,383
  • 1
  • 21
  • 32