2016-07-15 102 views
3

我在我的音樂播放器上製作了一個Android應用程序,其中我使用listview在我的設備中填充了所有歌曲,但是在Android 6.0中,我的應用程序崩潰,因爲我打開它。它不會在我給它存儲時崩潰在應用程序信息中手動設置權限,然後顯示我的歌曲。 我已經使用了android 6.0的請求權限,並且它導致要求用戶詢問存儲的訪問權限,但之後它不再顯示列表。我被困在這個...!我想要在應用運行時android Marshmallow請求權限..!

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()); 
    } 
} 
+1

檢查這裏的解決方案http://stackoverflow.com/a/37160573/842607 –

+0

在這裏檢查http://stackoverflow.com/a/38305951/5324829 – gaurang

+0

檢查這一個:http://stackoverflow.com/問題/ 37253397/android-permisson-request-code-issue/37253779#37253779 – Vickyexpert

回答

2

您可以使用,使處理運行權限輕鬆許多這個Android library

如果你的活動子類PermisoActivity,請求權限是一樣簡單:

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); 

搖籃

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

您可以添加多個權限,還請訪問link共享。您可以詳細瞭解所有信息。

2

此代碼將允許您在運行時請求權限,方法是檢查權限是否已被授予,如果沒有,請求用戶啓用權限。

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_RESPONSE); 
     } 

在這種情況下被檢查的權限是與位置服務有關。只需將ACCESS_COARSE_LOCATION替換爲您希望要求的任何許可。

相關問題