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
- <? 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" />
<?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
- <? 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="+"
- 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
- <? 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>
- <uses-sdk android:minsdkversion="3" />
- </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
- 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,int[] appwidgetids) {
- Context.startservice (new Intent (Context,updateservice. Class));
- }
- public static class Updateservice extends Service {
- @Override
- public void OnStart (Intent Intent, int startid) {
- Time Esttime = new Time ("EST");
- Esttime.settonow ();
- //Pass in the 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;
- }
- }
- }
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