用Kotlin写一个设置WebView在加载网页的时候在通知中显示进度
时间: 2023-09-05 09:05:39 浏览: 185
webView加载进度条显示
5星 · 资源好评率100%
### 回答1:
可以通过设置 `WebViewClient` 并在其回调方法中更新通知中的进度条来实现这个功能。下面是一个使用 Kotlin 实现的示例代码:
```kotlin
class MyWebViewClient(private val context: Context) : WebViewClient() {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val notificationBuilder = NotificationCompat.Builder(context, "webview_channel")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Loading website...")
.setProgress(100, 0, true)
.setOngoing(true)
.setOnlyAlertOnce(true)
private var notification: Notification? = null
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
showNotification()
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
hideNotification()
}
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
updateNotification(newProgress)
}
private fun showNotification() {
notification = notificationBuilder.build()
notificationManager.notify(1, notification)
}
private fun hideNotification() {
notificationManager.cancel(1)
}
private fun updateNotification(progress: Int) {
notificationBuilder.setProgress(100, progress, false)
notificationManager.notify(1, notificationBuilder.build())
}
}
```
在这个示例中,我们创建了一个 `MyWebViewClient` 类,它继承自 `WebViewClient`。在 `MyWebViewClient` 中,我们创建了一个通知,并在 `onPageStarted` 方法中显示它,在 `onPageFinished` 方法中隐藏它。在 `onProgressChanged` 方法中,我们更新通知中的进度条。还需要注意的是,我们使用了 Android 的通知框架来创建和管理通知。
可以在 `WebView` 中设置这个 `MyWebViewClient`,并在加载网页时调用:
```kotlin
val webView = findViewById<WebView>(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.webViewClient = MyWebViewClient(this)
webView.loadUrl("https://www.google.com/")
```
这样,在加载网页时,通知中就会显示进度条了。
### 回答2:
在Kotlin中,可以通过自定义WebViewClient来实现在加载网页时在通知中显示进度。
首先,在MainActivity.kt文件中,添加以下代码:
```kotlin
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
private lateinit var notificationManager: NotificationManagerCompat
private val channelId = "webview_channel_id"
private val notificationId = 123
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webView)
notificationManager = NotificationManagerCompat.from(this)
// 创建通知渠道
createNotificationChannel()
// 配置WebView
configureWebView()
// 加载网页
webView.loadUrl("https://www.example.com")
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Web View Notifications"
val descriptionText = "Shows web view progress in notification"
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
private fun configureWebView() {
webView.settings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
webView.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
// 更新通知进度
updateNotification(newProgress)
}
}
}
private fun updateNotification(progress: Int) {
val builder = NotificationCompat.Builder(this, channelId)
.setContentTitle("Web View Progress")
.setContentText("Loading...")
.setSmallIcon(R.drawable.ic_notification)
.setProgress(100, progress, false)
.setOnlyAlertOnce(true)
if (progress == 100) {
// 网页加载完成后移除通知
builder.setContentText("Load complete").setProgress(0, 0, false)
}
notificationManager.notify(notificationId, builder.build())
}
}
```
在activity_main.xml文件中添加以下代码:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
```
然后,在res/drawable目录下创建名为ic_notification.xml的文件,用作通知图标。在ic_notification.xml中添加以下代码:
```xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,14L12,7 6.71,11.29 9.71,11.29 12,8l2.29,3.29L17.29,11.29 12,11.29 12,14z" />
</vector>
```
以上代码中,我们创建了一个新的通知渠道,配置了WebView并实现了自定义的WebViewClient和WebChromeClient。在WebViewClient中,我们覆盖了shouldOverrideUrlLoading方法,以允许WebView加载URL。在WebChromeClient中,我们覆盖了onProgressChanged方法,以实时更新通知中的进度条。最后,我们使用NotificationCompat.Builder构建通知,并根据加载进度更新通知。
这样,当我们运行应用程序时,在加载网页时会在通知中显示进度。
### 回答3:
要用 Kotlin 写一个设置 WebView 在加载网页的时候在通知中显示进度的功能,可以按照以下步骤进行:
1. 首先,在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
```
2. 在活动中定义一个 WebView,并设置相关属性:
```kotlin
val webView: WebView = findViewById(R.id.webview)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
```
3. 创建一个 WebViewClient,并重写 onPageStarted 和 onPageFinished 方法来显示和隐藏进度通知:
```kotlin
webView.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
// 在通知栏显示进度
showProgressNotification()
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
// 隐藏进度通知
hideProgressNotification()
}
}
```
4. 实现显示进度通知的方法 showProgressNotification():
```kotlin
private fun showProgressNotification() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_LOW)
notificationManager.createNotificationChannel(channel)
}
val notificationBuilder = NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("页面加载中")
.setProgress(100, 0, false)
.setOngoing(true)
notificationManager.notify(1, notificationBuilder.build())
}
```
5. 实现隐藏进度通知的方法 hideProgressNotification():
```kotlin
private fun hideProgressNotification() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(1)
}
```
通过以上步骤,我们可以实现在 WebView 加载网页时,在通知栏中显示进度的功能。在 onPageStarted 方法中显示进度通知,在 onPageFinished 方法中隐藏进度通知。
阅读全文