Android Widget开发模板解析

移动开发
Android Widget开发模板是本文要介绍的内容,主要是来了解并学习Android Widget开发应用,具体内容的实现来看本文详解。

Android Widget开发模板是本文要介绍的内容,主要是来了解并学习Android Widget开发应用,Android Widget中使用了Java语言开发比W3C的Widget运行效率提高了不少,可以做更多的事情调用系统的API,除了UI上的限制外,我们可以考虑帮助系统完善一些appWidget,Android 123给出大家一个开发Widget的模板。

public class cwjWidget extends AppWidgetProvider {  
    @Override  
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
            int[] appWidgetIds) {  
                context.startService(new Intent(context, UpdateService.class)); //这里创建一个服务,防止出现等待超时对话框  
    }  
    public static class UpdateService extends Service {  //这个内部的服务我们推荐新开一个线程操作一些容易阻塞的情况,比如网络下载等等  
        @Override  
        public void onStart(Intent intent, int startId) {  
 
            RemoteViews updateViews = buildUpdate(this);  
 
            ComponentName thisWidget = new ComponentName(this, cwjWidget.class);  
            AppWidgetManager manager = AppWidgetManager.getInstance(this);  
            manager.updateAppWidget(thisWidget, updateViews);  
        }  
 
        public RemoteViews buildUpdate(Context context) {  
             Resources res = context.getResources();  
            RemoteViews updateViews = new RemoteViews(  
                context.getPackageName(), R.layout.main);  //主Widget的layout布局  
 
            PendingIntent pendingIntent = PendingIntent.getActivity(context,  
                    0 /* no requestCode */,  
                    new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS),  
                    0 /* no flags */);  
            updateViews.setOnClickPendingIntent(R.id.ui, pendingIntent); //单击view打开intent,目标为系统信息,就是上面的action位置  
 
            updateViews.setTextViewText(R.id.info,     
                android.os.Build.VERSION.CODENAME + " " +  
                android.os.Build.ID);   //这里是API的获取系统版本的方法  
 
            updateViews.setTextViewText(R.id.changelist,  
                android.os.Build.FINGERPRINT  
                );  
            return updateViews;  
        }  
        @Override  
        public IBinder onBind(Intent intent) {  
            return null;  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

有关涉及到的 androidmanifest.xml内容

<?xml version="1.0" encoding="utf-8"?> 
manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="com.android123.widget" 
   android:versionCode="1" 
   android:versionName="1.0"> 
 
   <uses-SDK android:minSdkVersion="3" /> 
   <application android:icon="@drawable/icon" android:label="@string/app_name"> 
       <receiver android:name=".BuildWidget" android:label="android123_cwj"> 
           <intent-filter> 
               <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
           </intent-filter> 
           <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /> 
       </receiver> 
       <service android:name=".cwjWidget$UpdateService" /> 
   </application> 
    
/manifest> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

android manifest.xml上面提到的  \res\xml\widget.xml文件内容

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
                          android:minWidth="150dip" 
                          android:minHeight="72dip"  
                          android:updatePeriodMillis="0" 
                          android:initialLayout="@layout/widget" />  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

有关 main.xml的内容为

<?xml version="1.0" encoding="utf-8"?> 
 
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/ui" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:orientation="vertical" 
   android:padding="6dip" 
   > 
 
   <TextView 
       android:id="@+id/info" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:textSize="18dip" 
       /> 
 
   <TextView 
       android:id="@+id/changelist" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:layout_marginTop="4dip" 
       android:textSize="9dip" 
       /> 
/LinearLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

小结:Android Widget开发模板解析的内容介绍完了,希望通过Android Widget开发内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-09-07 17:54:40

Android Wid开发

2011-09-07 14:39:47

Android Wid设计

2010-07-13 09:08:27

Widget开发

2011-09-07 14:01:41

Android Wid实例

2010-07-13 09:02:19

Widget开发

2011-09-09 20:14:58

Android Wid

2011-09-08 15:40:45

Android Wid组件

2010-07-23 08:54:02

2010-04-23 11:21:05

Widget开发

2011-09-07 13:30:48

Android WidTabWidget

2011-09-07 14:25:53

Android Wid设计

2011-09-07 13:00:36

2011-09-08 13:11:07

Android Wid实例

2011-09-07 11:15:25

2011-09-07 14:34:55

Android Wid控件

2011-09-09 17:59:26

QT Widget

2011-09-09 13:23:17

Widget

2011-09-09 19:23:52

Widget

2011-09-08 16:17:45

Widget

2011-09-08 14:21:37

jQueryWidget
点赞
收藏

51CTO技术栈公众号