help w AnalogClock widget - remoteview and appwidgetprovider

Tutorials with advanced 'difficulty' and more Lines of Code.

help w AnalogClock widget - remoteview and appwidgetprovider

Postby chuazy » Sun Nov 01, 2009 5:25 pm

Hi guys,

I am trying to develop and AnalogClock widget on the home screen that changes depending on the time of day. I am finding problems getting to the right code to make it happen, and will hope to seek you help and advise here.

1) Getting the dial resource in AnalogClock to change in code
I have been trying to get the AnalogClock in my widget XML file to change based on the system time. However, I realized that AppWidgetProvider do not support findViewbyID and i was trying to figure out how to make use of remoteviews to change the AnalogClock image. Any experts can enlighten me on this issue? Now I am implementing another method, but it is very ugly.


1) Getting onUpdate to work in appwidgetprovider
I am trying to get the onUpdate to work and change the views on set time, say at the start of every passing hour, at time zone change, at time change etc. Anyone know how i can get started to make onUpdate work for that purpose?

My code is below. Thanks guys. hope to learn from all of you.

Cheers
Zee

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Widget extends AppWidgetProvider
  2.  
  3. {    
  4.  
  5.     public void onReceive(Context context, Intent intent)
  6.  
  7.     {
  8.  
  9.         String action = intent.getAction();
  10.  
  11.         if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
  12.  
  13.         {              
  14.  
  15.                 setViews();            
  16.  
  17.  
  18.  
  19.             Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
  20.  
  21.             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
  22.  
  23.             views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
  24.  
  25.                                    
  26.  
  27.             AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);  
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33.    
  34.  
  35.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
  36.  
  37.     {
  38.  
  39.         setViews();
  40.  
  41.        
  42.  
  43.        
  44.  
  45.     }
  46.  
  47.    
  48.  
  49.     private void setViews(){
  50.  
  51.         GregorianCalendar Calendar = new GregorianCalendar();          
  52.  
  53.        
  54.  
  55.         int hour;
  56.  
  57.         hour = Calendar.get(Calendar.HOUR_OF_DAY);
  58.  
  59.        
  60.  
  61.         if (hour > 12){
  62.  
  63.                 hour -= 12;
  64.  
  65.         }
  66.  
  67.        
  68.  
  69.         if (hour == 0){
  70.  
  71.                 hour = 12;
  72.  
  73.         }
  74.  
  75.        
  76.  
  77.         switch (hour) {
  78.  
  79.                 case 1:
  80.  
  81.                         views = new RemoteViews("com.ClockWidget", R.layout.widget);
  82.  
  83.                         break;
  84.  
  85.  
  86.  
  87.                 case 2:
  88.  
  89.                         views = new RemoteViews("com.ClockWidget", R.layout.widget2);
  90.  
  91.                         break;
  92.  
  93.                        
  94.  
  95.                 case 3:
  96.  
  97.                         views = new RemoteViews("com.ClockWidget", R.layout.widget3);
  98.  
  99.                         break;
  100.  
  101.                        
  102.  
  103.                 case 4:
  104.  
  105.                         views = new RemoteViews("com.ClockWidget", R.layout.widget4);
  106.  
  107.                         break;
  108.  
  109.                        
  110.  
  111.                 case 5:
  112.  
  113.                         views = new RemoteViews("com.ClockWidget", R.layout.widget5);
  114.  
  115.                         break;
  116.  
  117.                        
  118.  
  119.                 case 6:
  120.  
  121.                         views = new RemoteViews("com.ClockWidget", R.layout.widget6);
  122.  
  123.                         break;
  124.  
  125.                        
  126.  
  127.                 case 7:
  128.  
  129.                         views = new RemoteViews("com.ClockWidget", R.layout.widget7);
  130.  
  131.                         break;
  132.  
  133.                        
  134.  
  135.                 case 8:
  136.  
  137.                         views = new RemoteViews("com.ClockWidget", R.layout.widget8);
  138.  
  139.                         break;
  140.  
  141.                        
  142.  
  143.                 case 9:
  144.  
  145.                         views = new RemoteViews("com.ClockWidget", R.layout.widget9);
  146.  
  147.                         break;
  148.  
  149.                        
  150.  
  151.                 case 10:
  152.  
  153.                         views = new RemoteViews("com.ClockWidget", R.layout.widget10);
  154.  
  155.                         break;
  156.  
  157.                        
  158.  
  159.                 case 11:
  160.  
  161.                         views = new RemoteViews("com.ClockWidget", R.layout.widget11);
  162.  
  163.                         break;
  164.  
  165.                        
  166.  
  167.                 case 12:
  168.  
  169.                         views = new RemoteViews("com.ClockWidget", R.layout.widget12);
  170.  
  171.                         break;
  172.  
  173.                        
  174.  
  175.                 default:
  176.  
  177.                         break;
  178.  
  179.                 }
  180.  
  181.  
  182.  
  183.     }
  184.  
  185.    
  186.  
  187.     private RemoteViews views;
  188.  
  189.     private AnalogClock clock;
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
chuazy
Freshman
Freshman
 
Posts: 2
Joined: Sat Dec 27, 2008 4:18 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests