gpt4 book ai didi

java - onActivityResult() 未调用(MapFragment)

转载 作者:行者123 更新时间:2023-12-01 17:50:24 26 4
gpt4 key购买 nike

我正在尝试使我的 map 应用程序重定向到位置权限设置,一旦授予权限,就会再次重定向到应用程序。由于某种原因,我的 onActivityResult 在启动 Activity Intent 后甚至没有被调用。基本上发生的情况是,当我单击出现的 snackbar 操作按钮时,它会启动 settingsIntent 并将我很好地重定向到设置,但 Activity 不会进入结果状态并且 onActivityResult 从未被调用过有什么建议吗?

这是代码:

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap

private fun checkPermissions(): Boolean {
//returns true if granted permission for location
var checker = ActivityCompat.checkSelfPermission(
applicationContext,
android.Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
applicationContext,
android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
if (checker) {
Log.d(TAG, "granted Permissions")
} else {
Log.d(TAG, "no Permissions granted")
}
return checker
}

@RequiresApi(Build.VERSION_CODES.M)
private fun requestPermissions() {
requestPermissions(
arrayOf(
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION
), PERMISSION_ID

)

}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (!checkPermissions()) {
Snackbar.make(
map.view!!,
"Please Enable Location Permission",
Snackbar.LENGTH_INDEFINITE
).setAction("Enable Location",
View.OnClickListener {
var settingsIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivityForResult(settingsIntent, 1)


})
.show()
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Log.d(TAG, "Redirected to settings for Location Permissions ")
if (resultCode == Activity.RESULT_OK) {
if (checkPermissions()) {
Log.d(TAG, "Permissions granted from settings")
startActivity(Intent(this, MapsActivity::class.java))
}
}
super.onActivityResult(requestCode, resultCode, data)
}

override fun onCreate(savedInstanceState: Bundle?) {

Log.d(TAG, "onCreate called")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)


}

@RequiresApi(Build.VERSION_CODES.M)
override fun onMapReady(googleMap: GoogleMap) {
Log.d(TAG, "omMapReady:starts")
mMap = googleMap
requestPermissions()

}
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.burgertracker">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />


<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>

</manifest>

最佳答案

这对我有用,

MainActivity

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<TextView>(R.id.tv_click).setOnClickListener {
val settingsIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivityForResult(settingsIntent, 1)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Toast.makeText(this, "Hakuna Mattat", Toast.LENGTH_SHORT).show()
}
}

activity_main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demointentapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

当我单击 TextView 时,我导航到“设置”页面上MainActivity当我从“设置”页面按回时,我看到 Toast留言Hakuna Mattat每次。

关于java - onActivityResult() 未调用(MapFragment),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60805892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com