I handle click events in a desktop widget this way:

   1. Create a service that inherits IntentService to handle updating the
   widget display
   2. For each item that you want to be clickable, create an intent filter
   entry in the manifest with a unique action name
   3. When you update the widget display in your service, do the following
   for each item that you want to handle a click event:
   1. Create an intent with the appropriate action from your manifest
      2. Use setOnClickPendingIntent() to call your service with the intent
      created above
      4. In the service's onHandleIntent() method, check the intent's
   action and update your widget's display based on the action

*Here is some example code from my manifest (Step 2):*

        <service
android:name=".services.RunningAppWgtUpdateService"
android:label="@string/app_widget_update_service"
android:exported="false"        >            <intent-filter>
     <action android:name=".WgtUpdateService.ACTION_NEXT" />
     <action android:name=".WgtUpdateService.ACTION_PREV" />
     <action android:name=".WgtUpdateService.ACTION_CLICK_0" />
        <action android:name=".WgtUpdateService.ACTION_CLICK_1" />
           <action android:name=".WgtUpdateService.ACTION_CLICK_2" />
              <action android:name=".WgtUpdateService.ACTION_CLICK_3"
/>                <action
android:name=".WgtUpdateService.ACTION_REFRESH" />
<action android:name=".WgtUpdateService.ACTION_REFRESH_NO_UPDATE" />
             <action android:name=".WgtUpdateService.ACTION_CLOSE_ALL"
/>                <action
android:name=".WgtUpdateService.ACTION_MODE_OPEN" />
<action android:name=".WgtUpdateService.ACTION_MODE_CLOSE" />
      <action android:name=".WgtUpdateService.ACTION_NO_OP" />
   </intent-filter>        </service>

*
Here is some example code from my service class to set the pending intent
(Step 3.1 & 3.2):*

        //RunningAppWgtUpdateService is the name of my class that
handles updating the display of my widget
        Intent newIntent = new Intent(context,
RunningAppWgtUpdateService.class);
newIntent.setAction("com.magouyaware.appswipe.WgtUpdateService.ACTION_MODE_OPEN");
       ViewHolder.s_curView.setOnClickPendingIntent(R.id.wgt_mode_open_btn,
PendingIntent.getService(this, 0, newIntent, 0));

*
Here is some example code from my service class to update the widget based
on the intent's action (Step 4):*

    @Override    protected void onHandleIntent(Intent intent)    {
   if (intent == null)            return;                String action
= intent.getAction();        if (action != null &&
action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_NO_OP"))
           return;                m_needsUpdate = true;        if
(action != null &&
action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_REFRESH_NO_UPDATE"))
           m_needsUpdate = false;                initializeUpdate();
             if
(!action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_PREV")
&&             
!action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_NEXT"))
       {            setIndeterminate();        }                if
(action != null &&
!action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_REFRESH"))
       {            if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_NEXT"))
               nextPage();            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_PREV"))
               prevPage();            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_CLICK_0"))
               processAppClick(m_app0Pkg);            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_CLICK_1"))
               processAppClick(m_app1Pkg);            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_CLICK_2"))
               processAppClick(m_app2Pkg);            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_CLICK_3"))
               processAppClick(m_app3Pkg);            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_CLOSE_ALL"))
               closeAllRunningApps();            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_MODE_OPEN"))
               setOpenMode(true);            else if
(action.equals("com.magouyaware.appswipe.WgtUpdateService.ACTION_MODE_CLOSE"))
               setOpenMode(false);        }
updateView();        finalizeUpdate();    }


Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Wed, Mar 14, 2012 at 3:59 AM, Pinheiro <[email protected]> wrote:

> Hi!
> Is there a way to have clickable elements inside a widget? (for instance,
> to change some text when an image is clicked)
>
> AFAIK, widgets only use "setOnClickPendingIntent()" that detects a click
> on all of the widget, it's not possible to set a method for a single
> element. Or is there?
>
> Thanks in advance!
>
> Pinheiro
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" 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-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" 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-developers?hl=en

Reply via email to