Android Desktop Widgets

Source: Internet
Author: User

A desktop part is a part of an application that is placed on the desktop using the Appwidget framework.

Res/layout/digitalclock.xml

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/time"
  4. android:textsize="45px"
  5. android:textstyle="bold"
  6. android:textcolor="#FFFFFFFF"
  7. android:background="@drawable/bg"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/time "    android:textsize=" 45px "    android:textstyle=" bold "    Android: Textcolor= "#FFFFFFFF"    android:background= "@drawable/bg"    android:layout_width= "Wrap_content    " android:layout_height= "Wrap_content"/>

Res/xml/est_appwidget.xml

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:minwidth="146dip"
  4. android:minheight="72dip"
  5. android:updateperiodmillis="+"
  6. android:initiallayout="@layout/digitalclock" />
<?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:updateperiodmillis=" 1000 "    android:initiallayout= "@layout/digitalclock"/>

Androidmanifest.xml

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.studio.android.chp10.ex4"
  4. android:versioncode="1"
  5. android:versionname="1.0">
  6. <application android:icon="@drawable/icon"
  7. android:label="@string/app_name">
  8. <receiver android:name="Esttime"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action
  12. android:name=
  13. "Android.appwidget.action.APPWIDGET_UPDATE" />
  14. </intent-filter>
  15. <meta-data android:name="Android.appwidget.provider"
  16. android:resource="@xml/est_appwidget" />
  17. </receiver>
  18. <service android:name=". Esttime$updateservice " />
  19. </Application>
  20. <uses-sdk android:minsdkversion="3" />
  21. </manifest>
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "      package=" com.studio.android.chp10.ex4 "      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>    

Java code
  1. Import Android.app.Service;
  2. Import Android.appwidget.AppWidgetManager;
  3. Import Android.appwidget.AppWidgetProvider;
  4. Import Android.content.ComponentName;
  5. Import Android.content.Context;
  6. Import android.content.Intent;
  7. Import Android.os.IBinder;
  8. Import Android.text.format.Time;
  9. Import Android.widget.RemoteViews;
  10. Public class Esttime extends Appwidgetprovider {
  11. @Override
  12. public void OnUpdate (context context,
  13. Appwidgetmanager Appwidgetmanager,int[] appwidgetids) {
  14. Context.startservice (new Intent (Context,updateservice.   Class));
  15. }
  16. public static class Updateservice extends Service {
  17. @Override
  18. public void OnStart (Intent Intent, int startid) {
  19. Time Esttime = new Time ("EST");
  20. Esttime.settonow ();
  21. //Pass in the package name and layout file
  22. Remoteviews updateviews = new Remoteviews (Getpackagename (), r.layout.digitalclock);
  23. Updateviews.settextviewtext (R.id.time,
  24. Esttime.format ("%h:%m"));
  25. ComponentName Thiswidget =
  26. New ComponentName (This, Esttime.   Class);
  27. Appwidgetmanager Manager =
  28. Appwidgetmanager.getinstance (this);
  29. Manager.updateappwidget (Thiswidget, updateviews);
  30. }
  31. @Override
  32. Public IBinder Onbind (Intent Intent) {
  33. return null;
  34. }
  35. }
  36. }
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 {@Override public void onUpdate (context context, Appwidgetmanager appwidgetmanager,in    T[] appwidgetids) {context.startservice (new Intent (Context,updateservice.class)); } public static class Updateservice extends Service {@Override public void OnStart (Intent Intent, int St            ArtID) {Time esttime = new Time ("EST");            Esttime.settonow ();            Incoming package name and layout file 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) {return null; }    }}

Esttime.rar

Android Desktop Widgets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.