Webview和IntentService+Notification的用法记录

Webview:

Webview常用设置:

webSettings.setUseWideViewPort(true)方法:// Webivew支持标签的viewport属性
webSettings.setLoadWithOverviewMode(true) 方法://缩放至屏幕的大小

webSettings.setJavaScriptEnabled(true) 方法://设置js代码可交互
webSettings.setAllowFileAccessFromFileURLs(true); 方法://是否允许通过file url加载的Javascript读取本地文件,默认值 false

addJavascriptInterface(object,“name”)方法:
第一个参数是供js调用的实例对象,类中的方法js可以直接通过第二个参数直接调用(window.nam.method直接调用)
第二个参数是第一个参数实例的别称,通过别称js可以直接调用对象中的方法

setWebViewClient(new WebViewClient())方法:
处理webview内部加载链接
当设置了此参数,所有的url链接默认将由webview加载,如需外部浏览器处理,需重写shouldOverrideUrlLoading方法,做相应拦截。(注”market://details?id=” 为应用市场独有的配置,会打开应用市场,类似这样的链接需要拦截做兼容处理)

onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)方法:回调可以直接返回 handler.proceed();忽略ssl证书。
onReceivedError回调,可在此处理加载出错的默认页面。

setWebChromeClient(new WebChromeClient())方法:
onProgressChanged回调,返回页面加载进度
onReceivedTitle回调,返回h5中ttile标签值
openFileChooser(ValueCallback uploadMsg, String acceptType)回调,响应h5中的file标签,通过第一个参数能将手机的文件返回给h5,注意个版本对应的方法差异

setDownloadListener(new DownLoadListeren());方法:
onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)回调,响应h5页面的下载事件(和webSettings.setAllowFileAccessFromFileURLs(true)一起设置)

基本设置这几个方法,webview就能满足大部分需求了

IntentService:
执行完任务后会自动销毁的服务,(一般用作后台下载)
onStartCommand (Intent intent, int flags, int startId)方法:每次startService时都会调用,
onHandleIntent(Intent intent)方法:运行在子线程中,
执行顺序 onCreate – onStartCommand – onHandleIntent – onDestory

Notification
通知栏简单用法:

1 . 初始化 NotificationManager
2 . 初始化 Notification
3 . 显示Notification

//初始化NotificationManager
notificationManger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//兼容8.0
    NotificationChannel channel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
     if (notificationManger != null) {
        notificationManger.createNotificationChannel(channel);
     }
  }

//初始化PendingIntent通知点击动作
Intent notificationIntent = new Intent();
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(title)
.setWhen(System.currentTimeMillis())
.setContentText(url)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setChannelId(PUSH_CHANNEL_ID)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(title);
//显示通知
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
notificationManger.notify(DownloadUtil.NOTIFY_ID_DOWNLOAD, notification);将通知显示到通知栏上

//下载时刷新通知栏进度条
mBuilder.setProgress(100, progress, false);
mBuilder.setContentInfo(progress + “%”);
notificationManger.notify(DownloadUtil.NOTIFY_ID_DOWNLOAD, mBuilder.build());

一般做后台下载时,IntentService和Notification会配合使用,在onStartCommand中显示通知,onHandleIntent中下载文件并更新通知上进度条进度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值