Java – I want to request permission while the application is running android Marshmallow ..!

I want to request permission while the application is running android Marshmallow ..!… here is a solution to the problem.

I want to request permission while the application is running android Marshmallow ..!

I

was making an android app on my music player where I filled all the songs in my device with ListView, but in android 6.0 my app crashed when I opened it. When I give it it it no longer crashes manually setting storage permissions in app info and then showing my songs.
I’ve used android 6.0 to request permission and it causes the user to request access to storage, but it no longer shows the list. I’m stuck in this…!

MainActivity.java

public class MainActivity extends AppCompatActivity implements MediaPlayerControl  {
    private ArrayList<Song> songList;
    private ListView songView;
    private MusicController controller;
    private MusicService musicSrv;
    private Intent playIntent;
    private boolean musicBound=false;
    private boolean paused=false, playbackPaused=false;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songView = (ListView) findViewById(R.id.song_list);
        songList = new ArrayList<Song>();

Android 6.0
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {

} else {

}
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1002);
    }
}
Android 6.0
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode){
        case 1002:
            if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                getSongList();
                 List songs in Alphabetical Order
                Collections.sort(songList, new Comparator<Song>() {
                    public int compare(Song a, Song b) {
                        return a.getTitle().compareTo(b.getTitle());
                    }
                });

SongAdapter songAdt = new SongAdapter(this, songList);
                songView.setAdapter(songAdt);
                setController();
            }else{

}
            return;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_shuffle:
            musicSrv.setShuffle();
            break;
        case R.id.action_end:
            stopService(playIntent);
            musicSrv = null;
            System.exit(0);
            break;
    }
    return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
    super.onStart();
    if(playIntent==null) {
        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        startService(playIntent);
    }
}

@Override
protected void onPause() {
    super.onPause();
    paused=true;
}

@Override
protected void onResume() {
    super.onResume();
    if(paused){
        setController();
        paused=false;
    }
}
@Override
protected void onStop() {
    controller.hide();
    super.onStop();
}
@Override
protected void onDestroy() {
    stopService(playIntent);
    musicSrv=null;
    super.onDestroy();
}
public void songPicked(View view){
    musicSrv.setSong(Integer.parseInt(view.getTag().toString()));
    musicSrv.playSong();
    if(playbackPaused){
        setController();
        playbackPaused=false;
    }controller.show(0);
}

connect to the service
private ServiceConnection musicConnection = new ServiceConnection(){

@Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicService.MusicBinder binder = (MusicService.MusicBinder)service;
        get service
        musicSrv = binder.getService();
        pass list
        musicSrv.setList(songList);
        musicBound = true;
    }

@Override
    public void onServiceDisconnected(ComponentName name) {
        musicBound = false;
    }
};

public void getSongList() {
    retrieve song info
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

if(musicCursor!=null && musicCursor.moveToFirst()){
        get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            songList.add(new Song(thisId, thisTitle, thisArtist));
        }
        while (musicCursor.moveToNext());
    }
}

Solution

You can use this android library to make it easier to handle runtime permissions.

If your activity is a subclass of PermisoActivity, requesting permission is straightforward:

    Permiso.getInstance().requestPermissions(new Permiso.IOnPermissionResult() {
    @Override
    public void onPermissionResult(Permiso.ResultSet resultSet) {
        if (resultSet.areAllPermissionsGranted()) {
             Permission granted!
        } else {
             Permission denied.
        }
    }

@Override
    public void onRationaleRequested(Permiso.IOnRationaleProvided callback, String... permissions) {
        Permiso.getInstance().showRationaleInDialog("Title", "Message", null, callback);
    }
}, Manifest.permission.READ_EXTERNAL_STORAGE);

Gradle

 dependencies {
    compile 'com.greysonparrelli.permiso:permiso:0.2.0'
}

You can add multiple permissions, also visit link share. You can get all the details.

Related Problems and Solutions