在应用中利用webview加载网页引起的内存高消耗

      android在应用中加载网页需要用到webview ,而webview需要加载在activity中显示,即webview要将显示内容绘制到activity上,由于为了避免webview对内存的消耗,一般在应用中需要动态加载,而不能再xml中显示的静态加载。
 
 
	webView = new MeetingWebView(this);

		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT);

		rel_webview.addView(webView, params);

      可以看到创建webview时候传入的是activity的context,大家知道应用进程不停止的话,对activity的引用会一直保存,所以这个webview的引用会被应用一直保持,不能被gc回收,这样就造成了app的内存泄露,如何解决呢...

     因为只要进程不终止,webview的引用会一直保持,所以我们可以将加载webview的activity在一个新的进程中启动,这样在activity ondestroy()时,退出该进程,那么webview的引用也将得到释放,内存也就不会泄露。

     而在一个新的进程中启动一个activity,可以参考下面资料:

    http://blog.csdn.net/luoshengyang/article/details/6720261

    最后在Ondestroy方法中显示的调用:

   

protected void onDestroy() {
super.onDestroy();
if (webView != null) {

webView.removeAllViews();

webView.destroy();
}
System.exit(0);
}


 参考:http://my.oschina.net/zhibuji/blog/100580

 


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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 方法隐藏进度通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值