zoukankan      html  css  js  c++  java
  • Android之Widget学习总结

    1.Widget设计步骤   需要修改三个XML,一个class:

      1)第一个xml是布局XML文件(如:main.xml),是这个widget的。一般来说如果用这个部件显示时间,那就只在这个布局XML中声明一个textview就OK了。

      2)第二个xml是widget_provider.xml,主要是用于声明一个appwidget的。其中,Layout就是指定上面那个main.xml。

      3)第三个xml是AndroidManifest.xml,注册broadcastReceiver信息。

      4)最后那个class用于做一些业务逻辑操作。让其继承类AppWidgetProvider。AppWidgetProvider中有许多方法,一般情况下我们只是覆写onUpdate(Context,AppWidgetManager,int[])方法。

    2.代码案例

      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是长什么样。

  • 相关阅读:
    Android应用性能测试之CPU和内存占用
    每天一个linux命令(30): chown命令
    安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
    android adb shell 命令大全
    adb logcat命令查看并过滤android输出log
    Ubuntu里字符编码设置
    linux下GBK->UTF-8文件编码批量转换脚本
    Android开发之如何保证Service不被杀掉(broadcast+system/app)
    android的m、mm、mmm编译命令的使用
    解决 samba不允许一个用户使用一个以上用户名与一个服务器或共享资源的多重连接
  • 原文地址:https://www.cnblogs.com/chengzhengfu/p/4579061.html
Copyright © 2011-2022 走看看