The developer documentation didn't seem to let me down. I can create a static widget without hesitation, I can even create a widget similar to the analog clock widget, which will be updated, however I canβt understand for life how to create a widget that it reacts to when the user clicks This. Here is the best example of code that the developer documentation gives to what should contain widget activity (the only other hint is API demos that only create a static widget):
public class ExampleAppWidgetProvider extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length;
from: Android developer documentation widget page
So, it looks like the pending intent is called up when the widget is clicked, which is based on the intent (I'm not quite sure what the difference is between the intent and the pending intent), and the intent is the ExampleActivity class. Therefore, I made my class of exercise classes a simple activity, which, when created, created a media player object and launched it (it will never release the object, so it will eventually crash, here is the code:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound); mp.start(); }
However, when I added the widget to the main screen and clicked on it, nothing played, in fact, nothing played, when I set the update timer for only a few hundred milliseconds (in the XML file of the application provider). In addition, I set breakpoints and found out that it not only did not reach activity, but no breakpoints that I set would ever occur. (I still do not understand why this is so), however, logcat seemed to indicate that the activity class file was running.
So, is there anything I can do to get appwidget to respond to a click? Since the onClickPendingIntent () method is the closest, I found a method like onClick.
Thank you very much.
java android android-widget onclick
Leif Andersen May 01 '10 at 1:58 a.m. 2010-05-01 01:58
source share