Advertisement
Guest User

AnalogClock Home widget

a guest
Apr 11th, 2012
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. appwidget_provider.xml
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
  4.    android:minWidth="294dp"  
  5.    android:minHeight="72dp"  
  6.    android:updatePeriodMillis="86400000"  
  7.    android:initialLayout="@layout/appwidget">  
  8. </appwidget-provider>
  9.  
  10. appwidget.xml
  11.  
  12. <?xml version="1.0" encoding="utf-8"?>  
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  14.    android:layout_width="fill_parent"  
  15.    android:layout_height="fill_parent"  
  16.    >  
  17.    <AnalogClock  
  18.       android:id="@+id/myAnalogClock"  
  19.       android:layout_width="wrap_content"  
  20.       android:layout_height="wrap_content">  
  21.    </AnalogClock>  
  22.    <TextView  
  23.       android:id="@+id/myText"  
  24.       android:layout_width="wrap_content"  
  25.       android:layout_height="wrap_content"/>  
  26. </LinearLayout>  
  27.  
  28. AppWidgetProvider.java
  29.  
  30. public class AppWidgetProdiverDemo extends AppWidgetProvider{  
  31.  
  32.     int mYear;  
  33.     int mMonth;  
  34.     int mDay;  
  35.     int mHour;  
  36.     int mMinutes;  
  37.     String str;  
  38.     @Override  
  39.     public void onDeleted(Context context, int[] appWidgetIds) {  
  40.         // TODO Auto-generated method stub  
  41.         super.onDeleted(context, appWidgetIds);  
  42.     }  
  43.  
  44.     @Override  
  45.     public void onDisabled(Context context) {  
  46.         // TODO Auto-generated method stub  
  47.         super.onDisabled(context);  
  48.     }  
  49.  
  50.     @Override  
  51.     public void onEnabled(Context context) {  
  52.         // TODO Auto-generated method stub  
  53.         super.onEnabled(context);  
  54.     }  
  55.  
  56.     @Override  
  57.     public void onReceive(Context context, Intent intent) {  
  58.         // TODO Auto-generated method stub  
  59.         super.onReceive(context, intent);  
  60.     }  
  61.  
  62.     @Override  
  63.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  64.             int[] appWidgetIds) {  
  65.         // TODO Auto-generated method stub  
  66.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  67.         Timer timer=new Timer();  
  68.         timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);  
  69.     }  
  70.  
  71.     class MyTime extends TimerTask{  
  72.         RemoteViews remoteViews;  
  73.         AppWidgetManager appWidgetManager;  
  74.         ComponentName thisWidget;  
  75.         public MyTime(Context context, AppWidgetManager appWidgetManager) {  
  76.             // TODO Auto-generated constructor stub  
  77.             this.appWidgetManager=appWidgetManager;  
  78.             remoteViews=new RemoteViews(context.getPackageName(),R.layout.appwidget);  
  79.             thisWidget=new ComponentName(context, AppWidgetProdiverDemo.class);  
  80.         }  
  81.         @Override  
  82.         public void run() {  
  83.             // TODO Auto-generated method stub  
  84.             try{  
  85.                 long time=System.currentTimeMillis();  
  86.                 Calendar mCalendar=Calendar.getInstance();  
  87.                 mCalendar.setTimeInMillis(time);  
  88.                 mYear=mCalendar.get(Calendar.YEAR);  
  89.                 mMonth=mCalendar.get(Calendar.MONTH);  
  90.                 mDay=mCalendar.get(Calendar.DAY_OF_MONTH);  
  91.                 mHour=mCalendar.get(Calendar.HOUR_OF_DAY);  
  92.                 mMinutes=mCalendar.get(Calendar.MINUTE);  
  93.                 str=mYear+"?"+mMonth+"?"+mDay+"?"+mHour+"?"+mMinutes+"?";  
  94.                 remoteViews.setTextViewText(R.id.myText,str);  
  95.                 appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
  96.             }catch(Exception e){  
  97.                 e.printStackTrace();  
  98.             }  
  99.         }  
  100.     }  
  101. }  
  102.  
  103. AndroidManifest.xml
  104.  
  105. <receiver android:name="AppWidgetProdiverDemo">  
  106.             <intent-filter>  
  107.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>  
  108.             </intent-filter>  
  109.             <meta-data android:name="android.appwidget.provider"  
  110.                 android:resource="@xml/appwidget_provider" />  
  111.         </receiver>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement