服务、广播与酷特性
2016-06-03 本文已影响25人
锋锋锋
Service:一直在后台运行,它没有用户界面,所以绝不会到前台来。
如果某个程序需要在运行时向用户呈现某种界面,或者该程序需要与用户交互,就需要使用Activity,否则就应该考虑使用Service了。
开发Service的步骤和开发Activity非常相似,开发Service的组件需要先开发一个Service子类,然后在androidManifest.xml文件配置,可通过配置<intent-filter.../>元素指定它可被那些Intent启动。
![](https://img.haomeiwen.com/i718595/d8119d1edad45aeb.png)
Service的框架组件有onCreate()、onStartCommand()、onDestroy()、onBind()等方法,重写这些方法只是简单地输出了一条字符串,如果希望Service组件做某些事情,那么只要在onCreate()或onStartCommand()方法中定义相关的功能代码即可。
![](http://upload-images.jianshu.io/upload_images/718595-c881d92bf0505671.png)
![](http://upload-images.jianshu.io/upload_images/718595-57f4a6816cff7fc7.png)
BroadcastReceiver
![](http://upload-images.jianshu.io/upload_images/718595-6342904c206252c4.png)
BroadcastReceiver有两种注册方式,分别是静态注册和动态注册
![](http://upload-images.jianshu.io/upload_images/718595-4d6c7121744f3c6f.png)
![](http://upload-images.jianshu.io/upload_images/718595-5eac8fccbdb95257.png)
动态注册
WebView
其下有三种方法实现
![](http://upload-images.jianshu.io/upload_images/718595-dd16a52182e90886.png)
![](http://upload-images.jianshu.io/upload_images/718595-601dfa99c922fe87.png)
![](http://upload-images.jianshu.io/upload_images/718595-ba8b24eb2d76b9e0.png)
![](http://upload-images.jianshu.io/upload_images/718595-419c6c6857854653.png)
![](http://upload-images.jianshu.io/upload_images/718595-9e5d8d9683bf3eee.png)
![](http://upload-images.jianshu.io/upload_images/718595-48a35fd826d53eb8.png)
![](http://upload-images.jianshu.io/upload_images/718595-f3f977a2a6f5af9b.png)
![](http://upload-images.jianshu.io/upload_images/718595-6f2c43631c87d602.png)
widget的注册:
![](http://upload-images.jianshu.io/upload_images/718595-70555030c0b306ae.png)
Widget的特殊XML
![](http://upload-images.jianshu.io/upload_images/718595-0f55beb27b6d3e39.png)
如何调试:
第一步:首先要开始调试状态。
![](http://upload-images.jianshu.io/upload_images/718595-6d3b1be997e09834.png)
第二步:输入网址chrome://inspect,找到相应的USB(要打开你的模拟器),点击inspect。
![](http://upload-images.jianshu.io/upload_images/718595-30837351ca84a13b.png)
更深入的使用:
![](http://upload-images.jianshu.io/upload_images/718595-4fc5dca605bfc3f2.png)
总结WebView:
![](http://upload-images.jianshu.io/upload_images/718595-73a1e71cf29ce385.png)
Widget:
![](http://upload-images.jianshu.io/upload_images/718595-aba4a06f729d54cd.png)
如何创建一个Widget呢,首先我们要先写一个TestWidget的类,这个类继承于AppWidgetProvider。
![](http://upload-images.jianshu.io/upload_images/718595-1387f2e0790efb70.png)
然后我们在Mainfest里面定义,定义一个action,一个meta-data,在resource定义一个layout,layout是一个xml。
![](http://upload-images.jianshu.io/upload_images/718595-dd46dac1b9f1f67e.png)
创建一个xml,在xml定义一个layout,还有它的一些配置。
![](http://upload-images.jianshu.io/upload_images/718595-7a88bd49d25b78d3.png)
Widget的onReceive和onUpdate。
![](http://upload-images.jianshu.io/upload_images/718595-bcb796e3afc4dfb4.png)
![](http://upload-images.jianshu.io/upload_images/718595-a42fb0b0bbfcf421.png)