如何检查用户是否已从 android 中的 activity/fragment 外部授予权限

How to check if user has granted permission from outside an activity/fragment in android

我正在使用 dexter 为导航组件提供运行时权限。

我有一个 activity 有多个片段。

在activity中,我想请求用户授予权限(存储)。

然后我有一个class(不是Activity或片段)需要在访问一些需要运行时权限的代码之前检查用户是否已授予权限..

我还有一个运行需要运行时权限的代码的 viewModel

问题

我无法访问其中任何一个的上下文 classes/viewModel。我现在如何检查用户是否已授予权限。

实用程序Class

fun requestExternalStoragePermission(context: Context, callback: PermissionsCallback) {
    Dexter.withContext(context) //
        .withPermissions(listOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE))
        .withListener(object : MultiplePermissionsListener {
            override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
                p0?.let {
                    if (p0.areAllPermissionsGranted()) {
                        callback.onPermissionRequest(granted = true)
                    } else {
                        callback.onPermissionRequest(granted = false)
                    }
                }
            }
            override fun onPermissionRationaleShouldBeShown(
                p0: MutableList<PermissionRequest>?,
                p1: PermissionToken?
            ) {
                p1?.continuePermissionRequest()
            }
        })
        .check()
}

界面

 interface PermissionsCallback {
  fun onPermissionRequest(granted: Boolean)
}

主要Activity

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    checkForPermission()
    setContentView(binding.root)
}

 private fun checkForPermission(){
  Utility.requestExternalStoragePermission(
      this,
      object : PermissionsCallback {
      override fun onPermissionRequest(granted: Boolean) {
      if (granted) {
       //good
       } else {
       //show settings
     }
}

已更新

音乐服务

class MusicService : MediaBrowserServiceCompat() {

 private val serviceJob = Job()
 private val serviceScope = CoroutineScope(Dispatchers.Main + 
serviceJob)

var context: Context? = null
   
    override fun onCreate() {
    super.onCreate()
    context = this
    checkForPermission()
  
 }

  private fun checkForPermission(){
    Utility.requestExternalStoragePermission(
        this,
        object : PermissionsCallback {
            override fun onPermissionRequest(granted: Boolean) {
                if (granted) {
                    //good, load songs
                    serviceScope.launch {
                        musicSource.fetchMediaData()
                    }
                } else {
                    //do something else
                    Toast.makeText(context, "We need permission nowwww", Toast.LENGTH_LONG)
                        .show()
                }
            }
        })
 }

 override fun onDestroy() {
    super.onDestroy()
    serviceScope.cancel()

    exoPlayer.removeListener(musicPlayerEventListener)
    exoPlayer.release()

}

}

ViewModel

 init {
    viewModelScope.launch {
        val allArtistes = artisteDatabase.getAllArtiste().....//I need to check here tooo
        _artisteList.value = Resource.success(allArtistes)
    }
}

fun doGetArtisteSongs(long: Long){
    viewModelScope.launch {
         val artisteSongs = artisteDatabase.getArtisteSongs(long).....//I need to check here too
        _artisteSongList.value = Resource.success(artisteSongs)
    }
}

我无法使用我的 Utility 函数检查 non-activiy/fragment class 和视图模型的权限,因为它需要 'context'.

正确的出路是什么?

如果 MusicService 是服务 class 您可以从服务获取上下文。

 Context context;
    
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
    }
    
 //then you can call requestExternalStoragePermission() using context.

 //If you want context in viewmodel use HILT dependency injection library.

 @ViewModelInject
    public MainViewModel(MainRepository mainRepository, @ApplicationContext Context context) {
        this.repository = mainRepository;
        this.context = context;

    }