授予运行时权限后,歌曲不会立即显示
Songs doesn't display instantly as soon as runtime permission is given
我正在制作一个音乐应用程序,我创建了一个从外部存储加载歌曲的功能。我已经授予棉花糖运行时权限,但问题是一旦我授予权限,歌曲就不会显示。但是当我停止应用程序并重新打开它时,我可以看到显示的歌曲。我想在授予运行时权限(从外部存储读取)后立即显示歌曲。谢谢!
Songs.java片段class:
public class Songs extends Fragment {
private static final String TAG = "Songs";
RecyclerView recyclerView;
private ArrayList<SongInfoModel> SongList = new ArrayList<SongInfoModel>();
SongAdapter songAdapter;
SongInfoModel s;
ScaleInAnimationAdapter alphaAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.songs_activity, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);
songAdapter = new SongAdapter(getContext(), SongList);
alphaAdapter = new ScaleInAnimationAdapter(songAdapter);
alphaAdapter.setDuration(1000);
alphaAdapter.setInterpolator(new OvershootInterpolator());
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(alphaAdapter);
Collections.sort(SongList, new Comparator<SongInfoModel>(){
public int compare(SongInfoModel a, SongInfoModel b){
return a.getSongName().compareTo(b.getSongName());
}
});
loadSongs();
return view;
}
public void loadSongs(){
ContentResolver resolver = getActivity().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor cursor = resolver.query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.DATA));
long duration = cursor.getLong(cursor.getColumnIndex
(MediaStore.Audio.Media.DURATION));
s = new SongInfoModel(name,artist,null,null,url,duration,null);
SongList.add(s);
}while (cursor.moveToNext());
}
cursor.close();
songAdapter = new SongAdapter(getContext(),SongList);
}
private void checkUserPermission(){
if(Build.VERSION.SDK_INT>=23){
if(ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE},123);
return;
}
}
loadSongs();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 123:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
loadSongs();
}else
Toast.makeText(getContext(), "Permission
Denied",Toast.LENGTH_SHORT).show();
checkUserPermission();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
}
已编辑问题的已编辑答案
在 onCreateView
方法中,在 return 语句之前,您调用了 loadSongs();
而不是调用 checkUserPermission();
旧答案
您还应该在方法 onRequestPermissionsResult
中执行 post 权限授予任务,方法是在 activity.
中覆盖它
您可以使用我的帮助程序库来摆脱它。 https://github.com/nabinbhandari/Android-Permissions
您必须覆盖此方法才能对允许或拒绝的权限执行操作。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
我正在制作一个音乐应用程序,我创建了一个从外部存储加载歌曲的功能。我已经授予棉花糖运行时权限,但问题是一旦我授予权限,歌曲就不会显示。但是当我停止应用程序并重新打开它时,我可以看到显示的歌曲。我想在授予运行时权限(从外部存储读取)后立即显示歌曲。谢谢!
Songs.java片段class:
public class Songs extends Fragment {
private static final String TAG = "Songs";
RecyclerView recyclerView;
private ArrayList<SongInfoModel> SongList = new ArrayList<SongInfoModel>();
SongAdapter songAdapter;
SongInfoModel s;
ScaleInAnimationAdapter alphaAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.songs_activity, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);
songAdapter = new SongAdapter(getContext(), SongList);
alphaAdapter = new ScaleInAnimationAdapter(songAdapter);
alphaAdapter.setDuration(1000);
alphaAdapter.setInterpolator(new OvershootInterpolator());
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(alphaAdapter);
Collections.sort(SongList, new Comparator<SongInfoModel>(){
public int compare(SongInfoModel a, SongInfoModel b){
return a.getSongName().compareTo(b.getSongName());
}
});
loadSongs();
return view;
}
public void loadSongs(){
ContentResolver resolver = getActivity().getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor cursor = resolver.query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndex
(MediaStore.Audio.Media.DATA));
long duration = cursor.getLong(cursor.getColumnIndex
(MediaStore.Audio.Media.DURATION));
s = new SongInfoModel(name,artist,null,null,url,duration,null);
SongList.add(s);
}while (cursor.moveToNext());
}
cursor.close();
songAdapter = new SongAdapter(getContext(),SongList);
}
private void checkUserPermission(){
if(Build.VERSION.SDK_INT>=23){
if(ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE},123);
return;
}
}
loadSongs();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 123:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
loadSongs();
}else
Toast.makeText(getContext(), "Permission
Denied",Toast.LENGTH_SHORT).show();
checkUserPermission();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
}
已编辑问题的已编辑答案
在 onCreateView
方法中,在 return 语句之前,您调用了 loadSongs();
而不是调用 checkUserPermission();
旧答案
您还应该在方法 onRequestPermissionsResult
中执行 post 权限授予任务,方法是在 activity.
您可以使用我的帮助程序库来摆脱它。 https://github.com/nabinbhandari/Android-Permissions
您必须覆盖此方法才能对允许或拒绝的权限执行操作。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}