fix: add Intent flag on open browser#599
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a BrowserUtils utility to centralize browser intent creation and error handling, which is integrated into MainScreen.kt. It also updates intent flags in Notification.kt. Review feedback recommends removing an unused import in BrowserUtils.kt, refactoring the intent creation into a reusable method to eliminate logic duplication between BrowserUtils and Notification.kt, and improving variable naming for better code clarity.
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.widget.Toast | ||
| import androidx.browser.customtabs.CustomTabsIntent |
Contributor
Comment on lines
+12
to
+27
| fun openBrowser(context: Context, url: String) { | ||
| val uri = url.toUri() | ||
| val intent = Intent(Intent.ACTION_VIEW, uri).apply { | ||
| addCategory(Intent.CATEGORY_BROWSABLE) | ||
| setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
| } | ||
| try { | ||
| context.startActivity(intent) | ||
| } catch (e2: ActivityNotFoundException) { | ||
| Toast.makeText( | ||
| context, | ||
| context.getString(R.string.could_not_open_browser), | ||
| Toast.LENGTH_SHORT | ||
| ).show() | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
建议将 Intent 的创建逻辑提取到一个独立的方法中,这样可以在 Notification.kt 等其他地方复用,避免逻辑重复。同时,建议将异常变量名 e2 修改为更符合规范的 e。
fun createBrowserIntent(url: String): Intent =
Intent(Intent.ACTION_VIEW, url.toUri()).apply {
addCategory(Intent.CATEGORY_BROWSABLE)
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
fun openBrowser(context: Context, url: String) {
try {
context.startActivity(createBrowserIntent(url))
} catch (e: ActivityNotFoundException) {
Toast.makeText(
context,
context.getString(R.string.could_not_open_browser),
Toast.LENGTH_SHORT
).show()
}
}
Comment on lines
+80
to
+83
| Intent(Intent.ACTION_VIEW, context.getString(R.string.local_url).toUri()).apply { | ||
| addCategory(Intent.CATEGORY_BROWSABLE) | ||
| setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
| } |
Contributor
MakinoharaShoko
approved these changes
May 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
解决某些安卓设备无法打开浏览器的问题