Skip to content

Instantly share code, notes, and snippets.

@davenotdavid
Last active February 28, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davenotdavid/176c3d9c74f138ee18512903c1bbe931 to your computer and use it in GitHub Desktop.
Save davenotdavid/176c3d9c74f138ee18512903c1bbe931 to your computer and use it in GitHub Desktop.
Requesting/granting in-app external storage permissions for devices Marshmallow (6.0/API 23) and above for a sample music player app (https://code.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764)
/**
* Code snippet for requesting/granting permissions properly for dangerous permissions
* such as external storage for devices M (6.0/API 23) and above. For sampling purposes,
* snippet includes only the onCreate() and onRequestPermissionsResult() methods within
* MainActivity.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Requests permission for devices with versions Marshmallow (M)/API 23 or above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSION_CODE);
return;
}
}
/**
* The following code either executes for versions older than M, or until the user
* initially accepts the in-app permission for the next sessions.
*/
init(); // Instantiating method/setup UI.
}
// Displays a permission dialog when requested for devices M and above.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSION_CODE) {
// User accepts the permission(s).
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Instantiating method/setup UI.
init();
// User denies the permission(s).
} else {
Toast.makeText(this, "Please grant the permissions for Music Player 2.0 and come" +
" back again soon!", Toast.LENGTH_SHORT).show();
// Runs a thread for a slight delay prior to shutting down the app.
Thread mthread = new Thread() {
@Override
public void run() {
try {
sleep(1500);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mthread.start();
}
}
}
private void init() {
songList = new ArrayList<Song>();
songView = (ListView) findViewById(R.id.song_list);
// Invokes the iteration for adding songs.
getSongList();
// Sorts the data so that the song titles are presented alphabetically.
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
// Custom-base adapter instantiation that displays the songs via the ListView.
SongAdapter songAdapter = new SongAdapter(this, songList);
songView.setAdapter(songAdapter);
// Initially passes the song list when the user first accepts the permission.
mMusicService.setList(songList);
// Sets the music controller up.
setController();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment