Custom widgets in Android

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; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); views.setOnClickPendingIntent(R.id.button, pendingIntent); // Tell the AppWidgetManager to perform an update on the current App Widget appWidgetManager.updateAppWidget(appWidgetId, views); } } } 

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.

+59
java android android-widget onclick
May 01 '10 at 1:58 a.m.
source share
1 answer

First add a static variable with a constant.

 public static String YOUR_AWESOME_ACTION = "YourAwesomeAction"; 

Then you need to add the action to the intent before adding the intent to the pending intent:

 Intent intent = new Intent(context, widget.class); intent.setAction(YOUR_AWESOME_ACTION); 

(Where widget.class is the class of your AppWidgetProvider, your current class)

Then you need to create a PendingIntent with getBroadcast

 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

Set onClickPendingIntent for interactive viewing in your widget

 remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent); 

Then override the onReceive method in the same class:

 @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); 

And then respond to the button by pressing the button, requesting the intention returned for your action in the onReceive method:

 if (intent.getAction().equals(YOUR_AWESOME_ACTION)) { //do some really cool stuff here } 

And it must be!

+123
May 01 '10 at 3:01 a.m.
source share



All Articles