zoukankan      html  css  js  c++  java
  • Android Widget搭建过程

    Android平台下Widget的搭建过程为:

    1.在res/layout下创建Widget的布局文件:digitalclock.xml

    代码
    <?xml version="1.0" encoding="UTF-8"?>
    <TextView  
        
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width
    ="wrap_content" 
        android:layout_height
    ="wrap_content" 
        android:id
    ="@+id/time"
        android:textSize
    ="45px"
        android:scrollX
    ="30px"
        android:scrollY
    ="30px"
        android:textStyle
    ="bold"
        android:textColor
    ="#ff000000"
        android:background
    ="@drawable/bg"
        
    />

    2.在res/xml下创建Widget的描述文件:est_appwidget.xml

    代码
    <?xml version="1.0" encoding="UTF-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth
    ="186dip"
        android:minHeight
    ="212dip"
        android:updatePeriodMillis
    ="1000"
        android:initialLayout
    ="@layout/digitalclock"/>

    3. 从AppWidgetProvider继承一个类(ESTTime),重写其虚方法

    代码
    package com.android.test.esttime;

    import android.app.Service;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.os.IBinder;
    import android.text.format.Time;
    import android.widget.RemoteViews;


    public class ESTTime extends AppWidgetProvider {
        
        
    /** Called when the activity is first created. */
        @Override
        
    public void onUpdate(Context context,
                AppWidgetManager appWidgetManager,
    int[] appWidgetIds )
        {
            context.startService(
    new Intent(context,UpdateService.class));        
        }
        
      
    //Service类
        public static class UpdateService extends Service {
            @Override
            
    public void onStart(Intent intent,int startId){
                Time estTime 
    = new Time("EST");
                estTime.setToNow();
                RemoteViews updateViews 
    = 
                    
    new RemoteViews(getPackageName(),
                            R.layout.digitalclock);
                updateViews.setTextViewText(R.id.time, estTime.format(
    "%H:%M"));
                
                ComponentName thisWidget 
    = new ComponentName(this,ESTTime.class);
                
                AppWidgetManager manager 
    = AppWidgetManager.getInstance(this);
                manager.updateAppWidget(thisWidget, updateViews);
            }

            @Override
            
    public IBinder onBind(Intent intent) {
                
    // TODO Auto-generated method stub
                return null;
            }

        }
        

    }

    4.在AndroidManifest.xml中注册本Widget 

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package
    ="com.android.test.esttime"
          android:versionCode
    ="1"
          android:versionName
    ="1.0">
        
    <application android:icon="@drawable/icon" android:label="@string/app_name">
            
    <receiver android:name=".ESTTime"
                      android:label
    ="@string/app_name">
                
    <intent-filter>
                    
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                
    </intent-filter>
                
    <meta-data android:name="android.appwidget.provider" 
                android:resource
    ="@xml/est_appwidget"/>            
            
    </receiver>
            
    <service android:name=".ESTTime$UpdateService"/>
        
    </application>
        
    <uses-sdk android:minSdkVersion="3" />
    </manifest> 

    最后,编译运行。这样在Home上长按弹出的Widget列表中就有新创建的Weiget了 。

  • 相关阅读:
    Redux其实很简单(原理篇)
    基于Docker的UI自动化初探
    视觉设计师的进化
    浅谈容器监控和网易云计算基础服务实践
    微服务实践沙龙-上海站
    知物由学 | 见招拆招,Android应用破解及防护秘籍
    6本互联网技术畅销书免费送(数据分析、深度学习、编程语言)!
    Lily-一个埋点管理工具
    ArcGIS 10 许可配置
    How to Programmatically Add/Delete Custom Options in Magento?
  • 原文地址:https://www.cnblogs.com/alwaysyouare/p/1640219.html
Copyright © 2011-2022 走看看