当前位置: 移动技术网 > IT编程>移动开发>Android > 讲解Android中的Widget及AppWidget小工具的创建实例

讲解Android中的Widget及AppWidget小工具的创建实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

杨培安我相信伴奏,防城港汽车站,幼女交

1.widget 、app widget 、web app 的概念

widget最初的概念是98年一个叫rose的苹果工程师提出,直到2003年的时候才正式为大家所知,不过随后无数大公司都开始接受并应用这一思路。 现在我们看到在苹果系统里按下f4弹出的dashboard里的小工具叫widget,在windows 7里侧边栏上的那些漂亮的小工具叫gadget(widget变体?),除此以外还有yahoo widget等等widget产品。他们有一个共同的特点就是采用前台web开发用的技术(譬如html、css、javascript)来制作的小工 具、小部件。

在android系统里,几乎每个可视化的view组件都叫widget,起这个名字可能当时是为了赶时髦。

app widget是从android 1.5以后才有的东东,就是可以放在android桌面上的应用程序小组件。这一点上看他的功能很像windows的侧边栏小工具,可惜的是他的采用技术 并不是html等技术。当然app widget才是我们本讲的主角,本来他应该顺理成章叫做widget的,至少也要叫做gadget吧,可惜这个名字已经被他自己的系统占用了,所以只好 改名叫app widget。

最后讲一下web app 或者说是android web application,也许叫mobile web application 更准确些。我们发现现在智能机系统平台很多,譬如ios、android、windows phone 、webos、blackberry等等,它们采用的技术框架也各不相同,有没有办法写一个程序在各个系统上都能运行呢?答案是肯定的,写基于 webkit的浏览器的应用即可。我们使用 html5、css3、javascript、webkit 等技术来写的web application也许是今后的一个大潮流也说不准啊。有机会我们再讲讲android web application 的开发。

2.创建一个最简单的widget

代码案例:

1)main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <textview android:id="@+id/tvcurrtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" 
    android:textcolor="@color/black"/> 
</linearlayout> 

2)hello_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
  android:minwidth="146dip" android:minheight="72dip" android:initiallayout="@layout/main"> 
</appwidget-provider> 

3)androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.woody.testwidget" android:versioncode="1" 
   android:versionname="1.0"> 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:name=".hellowidgetprovider" android:label="@string/app_name"> <!-- hellowidgetprovider为那个class(业务处理) --> 
      <intent-filter> 
        <action android:name="android.appwidget.action.appwidget_update" /> <!-- 指定了的 --> 
      </intent-filter> 
      <meta-data android:name="android.appwidget.provider"android:resource="@xml/hello_widget_provider" /> <!-- 为上面指定了的widget --> 
    </receiver> 
  </application> 
</manifest> 

4)hellowidgetprovider.java

public class hellowidgetprovider extends appwidgetprovider { 
  /** called when the activity is first created. */ 
  @override 
  public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { 
    timer timer = new timer(); 
    timer.scheduleatfixedrate(new mytime(context, appwidgetmanager), 1, 1000); 
  } 

  public class mytime extends timertask { 
    remoteviews remoteviews; 
    appwidgetmanager appwidgetmanager; 
    componentname thiswidget; 
    dateformat format = simpledateformat.gettimeinstance(simpledateformat.medium, locale.getdefault());   
    public mytime(context context, appwidgetmanager appwidgetmanager) { 
      this.appwidgetmanager = appwidgetmanager; 
      remoteviews = new remoteviews(context.getpackagename(), r.layout.main); 
      thiswidget = new componentname(context, hellowidgetprovider.class); 
    } 
    @override 
    public void run() { 
      remoteviews.settextviewtext(r.id.tvcurrtime, "time = " + format.format(new date())); 
      appwidgetmanager.updateappwidget(thiswidget, remoteviews); 
    } 
  } 
}  

代码解释:remoteview是用来描述一个跨进程显示的view,也就是说这个view是在另外一个进程显示的。它inflate于layout资源文件。并且提供了可以修改过view内容的一些简单基础的操作。

appwidget---remoteview,appwidgetprovider是一个brocasereceiver,只是接受到enable, update,disale,delete这些message,而真正显示界面的是appwidgethostview(这是在launcher里面实现的),这中间就是通过remoteview来沟通。通过remoteview告诉launcher你想要的appwidget是长什么样。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网