views:

618

answers:

2

Hi, i have 2 questions about widgets update

  • I have 2 buttons and i need to change one button text when i press the other one, how can i do this?
  • The first time i open the widget it calls the onUpdate method, but it never calls it again. I need to update the widget every 2 seconds and i have this line in the xml.

    android:updatePeriodMillis="2000"

Do i need a service or should it works just with the updatePeriodMillis tag?

  • onUpdate method

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.newswidget);
    
    
    Intent intent = new Intent(context, DetalleConsulta.class);
    intent.putExtra(DetalleConsulta.CONSULTA_ID_NAME, "3");
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.btNews, pendingIntent);
    
    
    /* Inicializa variables para llamar el controlador */
    this.imei = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    this.controlador = new Controlador(this.imei);
    
    
    try {
        this.respuestas = this.controlador.recuperarNuevasRespuestas();
    
    
    
    if(this.respuestas != null && this.respuestas.size() > 0){
        Iterator<Consulta> iterRespuestas = this.respuestas.iterator();
        views.setTextViewText(R.id.btNews, ((Consulta)iterRespuestas.next()).getRespuesta());
    }
    
    } catch (PersistenciaException e) { //TODO manejar error } appWidgetManager.updateAppWidget(appWidgetIds, views);

thx a lot!!!

A: 

hi,

  • about the first question i found a great example about handle widget events:

Handling User Interaction with Android App Widgets

  • about the second one i was reading on the web and i found that in android 1.6 the minimun time to update the widget is 30 min.

I tried it and it works.

i hope this help others!!!

david
A: 

hehehe yes that was changed in since donut(1.6) to avoid your battery will be consumed shortly.

Hey David the next time try to put your other question in different thread to be accord with stackoverflow.com =)

to update the text from another button...

 final    Button btn1 = (Button) this.findViewById(R.id.Button01);
   final Button btn2 = (Button) this.findViewById(R.id.Button02);

   btn1.setOnClickListener(new OnClickListener() {                      
            @Override
            public void onClick(View v) {
                btn2.setText("Button1 changed your text!");
            }           
        });      
Jorgesys
Hi, thx for the advive, next time i'll make the questions in different threads. About the answer i want to do this but in my application widget, the answer is in the link i post in my own answer... thx...
david