public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { if (requestCode == PERMISSION_ReadExternalStorage) { if (grantResults[0] == Permission.Granted) { Songleton.Instance.AllSongs = MusicRetrieval.LoadSongs(this); SongListFragment songListFragment = new SongListFragment(); //MusicControlFragment musicControlFragment = new MusicControlFragment (); MusicControls musicControls = new MusicControls(); //var trans = FragmentManager.BeginTransaction ();以下两条语句极度重要,解决大问题! Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager; Android.Support.V4.App.FragmentTransaction trans = fragmentManager.BeginTransaction(); trans.Add(Resource.Id.fragment_container, songListFragment, "songListFragment"); trans.Add(Resource.Id.music_controls_container, musicControls, "musicControls"); trans.Commit(); if (Build.VERSION.SdkInt != Android.OS.BuildVersionCodes.NMr1) { Toast.MakeText(this, "读取数据权限已申请", ToastLength.Short).Show(); } } else { if (Build.VERSION.SdkInt != Android.OS.BuildVersionCodes.NMr1) { Toast.MakeText(this, "申请读取数据权限被拒", ToastLength.Short).Show(); } } } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); //java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=17271, uid=10085 requires android.permission.READ_EXTERNAL_STORAGE //6.0及以上需要动态申请权限 if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M) { //RequestWriteSettings(); bool isGrant = ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted; if (isGrant == false) { ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, PERMISSION_ReadExternalStorage); } else { Songleton.Instance.AllSongs = MusicRetrieval.LoadSongs(this); if (savedInstanceState == null) { SongListFragment songListFragment = new SongListFragment(); //MusicControlFragment musicControlFragment = new MusicControlFragment (); MusicControls musicControls = new MusicControls(); //var trans = FragmentManager.BeginTransaction ();以下两条语句极度重要,解决大问题! Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager; Android.Support.V4.App.FragmentTransaction trans = fragmentManager.BeginTransaction(); trans.Add(Resource.Id.fragment_container, songListFragment, "songListFragment"); trans.Add(Resource.Id.music_controls_container, musicControls, "musicControls"); trans.Commit(); } //在程序中获取string.xml中字符串和数值 Toast.MakeText(this, this.Resources.GetString(Resource.String.scan_songs) + Songleton.Instance.AllSongs.Count.ToString(), ToastLength.Short).Show(); } } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); Songleton.Instance.AllSongs = MusicRetrieval.LoadSongs(this); if (savedInstanceState == null) { SongListFragment songListFragment = new SongListFragment(); MusicControlFragment musicControlFragment = new MusicControlFragment(); var trans = FragmentManager.BeginTransaction(); trans.Add(Resource.Id.fragment_container, songListFragment); trans.Add(Resource.Id.music_controls_container, musicControlFragment); trans.Commit(); } }