我在 Android Studio (2022.3.1) 中创建了一个应用程序,它只包含一个 webview 并处理 url。 它一直有效,直到我通过意图添加深层链接。有人可以告诉我,这有什么问题吗?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FullscreenDark"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.FullscreenDark">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="@string/app_host" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt:
package com.app.myapp
import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val host = getString(R.string.app_host)
webView = findViewById(R.id.webview)
val webSettings: WebSettings = webView.settings
// Configure settings
webSettings.javaScriptEnabled = true // Enable JavaScript
webView.webViewClient = MyWebViewClient()
// webView.webViewClient = WebViewClient()
// Check for deep link
val intentData = intent.data
if (intentData != null) {
val deepLink = intentData.toString()
webView.loadUrl(deepLink)
} else {
// Load your default URL
webView.loadUrl("https://$host")
}
}
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressedDispatcher.onBackPressed()
}
}
private inner class MyWebViewClient : WebViewClient() {
@Deprecated("Deprecated in Java")
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
val host = getString(R.string.app_host)
// Load URLs from the same domain within the WebView
if (url != null && url.startsWith("https://$host")) {
view?.loadUrl(url)
return true
}
// Load all other URLs in the default browser
return false
}
}
}
我多次尝试从头开始重新设置它,并查找了其他示例。
当我运行它时,我每次都会收到此错误:
Unable to determine activity name
您在
<intent-filter>
下缺少这些拖线:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
这些拖线表示此 Activity 是您的应用程序的入口点,或者是您的应用程序启动时首先启动的 Activity。