Thanks for the answer,
I have tried working with my code and looking at your widget's source.
Event though, I can't get it working. I do not use a service as you,
but my code looks like this:
package com.kaloersoftware.kaloertimerwidget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
public class KaloerTimer extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager,
int[] appWidgetIds) {
Intent i = new Intent(context, KaloerTimer.class);
PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0);
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.main);
rv.setOnClickPendingIntent(R.id.startBtn, pi);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "It Works", Toast.LENGTH_SHORT).show();
}
}
Btw, is there any way to debug a widget? Nothing happens when I place
a breakpoint in the onUpdate() method.
//Kaloer
On 11 Jun., 18:35, "Mark Murphy" <[email protected]> wrote:
> > I'm trying to develop my first home screen widget for android, but
> > don't know how to use buttons. I've followed the tutorial
> >http://android-developers.blogspot.com/2009/04/introducing-home-scree...
> > but it doesn't show how to use buttons. Somewhere I read about
> > RemoteViews, PendingIntents and the setOnClickPendingIntent method,
> > but I don't really know how to use them.
> > Therefor, I have some questions:
> > 1) How do I set an onClickListener (or an OnClickPendingIntent() ) for
> > the button?
>
> In the tutorial you cite, Jeff Sharkey uses setOnClickPendingIntent() for
> clicks on the widget as a whole. However, that works just as well for
> clicks just on a button -- provide the button's ID as the first parameter.
>
> > 1,2) where do I set this? There is no onCreate() method..
>
> You would set it wherever you are building your RemoteViews.
>
> > 2) This method starts an Intent, but how do I use an Intent to just
> > run a method? I do not want to start any activity.
>
> You can't use an Intent to "just run a method". However, an Intent does
> not need to start an activity. Jeff's example uses
> PendingIntent.getActivity() to launch an activity, but you could use
> PendingIntent.getService() or PendingIntent.getBroadcast() to communicate
> with other components.
>
> I can point you at some source code for an app widget that demonstrates
> the use of buttons communicating with a back-end service -- visit:
>
> http://commonsware.com/AdvAndroid/
>
> and download the source code to my one book. In the ZIP file, you will
> find an AppWidget/TwitterWidget project. As the name suggests, it is a
> widget that displays the latest tweet in your friends' timeline. It also
> has a button to refresh and a button to configure the widget. The
> configure button pops an activity, but the refresh button triggers the
> same logic as is invoked when updateTimeMillis elapses.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com
> _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---