我正在運行有關設定權限的錯誤。我在清單中宣告了位置和互聯網權限,并呼叫checkPermission()并onRequestPermissionsResult顯示權限對話框。但是,我無法啟動我的應用程式并且收到相同的錯誤。
我試過了
private val hasFineLocationPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
我將上述行的背景關系更改為 applicationContext、this@MainActivity、MainActivity@This,但我仍然收到相同的錯誤。
主要活動
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
private var REQUIRED_PERMISSIONS = arrayOf<String>(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
private val context : Context = applicationContext
private val hasFineLocationPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
private val hasBackgroundLocationPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkPermission()
}
fun checkPermission() {
if (hasFineLocationPermission == PackageManager.PERMISSION_GRANTED
) {
// Fine Location permission is granted
// Check if current android version >= 11, if >= 11 check for Background Location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (hasBackgroundLocationPermission == PackageManager.PERMISSION_GRANTED
) {
// Background Location Permission is granted so do your work here
} else {
// Ask for Background Location Permission
askPermissionForBackgroundUsage()
}
}
} else {
// Fine Location Permission is not granted so ask for permission
askForLocationPermission();
}
}
private fun askForLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
) {
AlertDialog.Builder(this)
.setTitle("Permission Needed!")
.setMessage("Location Permission Needed!")
.setPositiveButton("OK", DialogInterface.OnClickListener { dialogInterface, int ->
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION), hasFineLocationPermission)
})
.setNegativeButton("CANCEL", DialogInterface.OnClickListener { dialogInterface, int ->
// Permission is denied by the user
throw RuntimeException("denied")
})
.create().show()
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), hasFineLocationPermission)
}
}
private fun askPermissionForBackgroundUsage() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity,
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
) {
AlertDialog.Builder(this)
.setTitle("Permission Needed!")
.setMessage("Background Location Permission Needed!, tap \"Allow all time in the next screen\"")
.setPositiveButton("OK", DialogInterface.OnClickListener { dialogInterface, int ->
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), hasBackgroundLocationPermission)
})
.setNegativeButton("CANCEL", DialogInterface.OnClickListener { dialogInterface, int ->
// Permission is denied by the user
throw RuntimeException("denied")
})
.create().show()
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), hasBackgroundLocationPermission)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// if (requestCode == PERMISSIONS_REQUEST_CODE && grantResults.size ==
REQUIRED_PERMISSIONS.size) {
if (requestCode == hasFineLocationPermission) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
if (hasBackgroundLocationPermission == PackageManager.PERMISSION_GRANTED) {
// var check_result = true
// for (result in grantResults) {
// if (result != PackageManager.PERMISSION_GRANTED) {
// check_result = false;
// break;
// }
// }
} else {
askPermissionForBackgroundUsage()
}
}
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
REQUIRED_PERMISSIONS[0])
) {
Toast.makeText(this,
"denied",
Toast.LENGTH_SHORT).show()
finish()
} else {
Toast.makeText(this, "denied", Toast.LENGTH_SHORT)
.show()
}
}
} else if (requestCode == hasBackgroundLocationPermission) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// User granted for Background Location Permission.
} else {
ActivityCompat.shouldShowRequestPermissionRationale(this,
REQUIRED_PERMISSIONS[1])
}
}
}
}
uj5u.com熱心網友回復:
在呼叫 onCreate 之前,活動不會被初始化。這意味著在那之前你不能使用像 getApplicationContext 這樣的函式。基本上,活動不應該有初始化塊。如果依賴于背景關系,他們應該在 onCreate 中運行所有內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/471534.html
