Android widget Click Event

Source: Internet
Author: User

 

In appwidget, both imagebutton and button are supported controls, and their events can be divided into three types:
1. Enable Activity
Ii. Start Service
3. Send button action
The following describes how to implement the analysis one by one.

1. Enable Activity
1. First, define an intent for enabling the activity.
Eg:

Intent fullintent = new intent (this, fullscreen. Class );
To pass data, use the intent. putextra () method
Eg: Fullintent. putextra ("iscircle", iscircle );

2. instantiate a pendingintent with intent and call the getacticity method of pendingintent to start another activity.
① If the intent has data, set the value of the last parameter to flag_cancel_current.
Eg:Pendingintent pfullintent = pendingintent. getactivity (this, 0, fullintent, pendingintent. flag_cancel_current );
② If the intent does not contain data, the last parameter is set to 0.
Eg:Pendingintent pfullintent = pendingintent. getactivity (this, 0, fullintent, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibfullscreen, pfullintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

2. Enable Service
1. Define an intent to enable the Service
Eg:Intent startserviceinten = newintent ("ZYF. Temp. Service. Start ");
Note: The parameter is the service enabling action.

2. instantiate a pendingintent with intent and start a service using the getservice method of pendingintent
Eg:Pendingintent pintent = pendingintent. getservice (context, 0, startserviceinten, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibfullscreen, pfullintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

3. Send button action
1. Define an intent to send the button action
Eg:Intent previnten = new intent ("Prev ");

2. instantiate a pendingintent with intent and use the getbroadcast method of pendingintent to send broadcasts.
Eg:Pendingintent pprevintent = pendingintent. getbroadcast (this, 0, previnten, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibprev, pprevintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

6. Receive the action
① Receive in appwidget's onreceive Method
Actions: Add action to manifest. xml.
Eg:<Intent-filter>
     <Actionandroid: Name = "android. appwidget. Action. appwidget_update"> </Action>
     <Actionandroid: Name = "Prev"> </Action>
   </Intent-filter>
Compile the action to be implemented in the onreceive () method
Eg:If (intent. getaction (). Equals ("Prev "))
{
  // Write the action to be implemented after receiving the action
}
② Receive in Service
Register a broadcastreceive and declare the receiver.
Eg:Intentfilter filter = new intentfilter ();
    Filter. addaction ("Prev ");
    Registerreceiver (docommand, filter );
Compile, write the action to be implemented in the onreceive method of the broadcastreceive class
Eg:If (intent. getaction (). Equals ("Prev "))
{
  // Write the action to be implemented after receiving the action
}

In appwidget, both imagebutton and button are supported controls, and their events can be divided into three types:
1. Enable Activity
Ii. Start Service
3. Send button action
The following describes how to implement the analysis one by one.

1. Enable Activity
1. First, define an intent for enabling the activity.
Eg:

Intent fullintent = new intent (this, fullscreen. Class );
To pass data, use the intent. putextra () method
Eg: Fullintent. putextra ("iscircle", iscircle );

2. instantiate a pendingintent with intent and call the getacticity method of pendingintent to start another activity.
① If the intent has data, set the value of the last parameter to flag_cancel_current.
Eg:Pendingintent pfullintent = pendingintent. getactivity (this, 0, fullintent, pendingintent. flag_cancel_current );
② If the intent does not contain data, the last parameter is set to 0.
Eg:Pendingintent pfullintent = pendingintent. getactivity (this, 0, fullintent, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibfullscreen, pfullintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

2. Enable Service
1. Define an intent to enable the Service
Eg:Intent startserviceinten = newintent ("ZYF. Temp. Service. Start ");
Note: The parameter is the service enabling action.

2. instantiate a pendingintent with intent and start a service using the getservice method of pendingintent
Eg:Pendingintent pintent = pendingintent. getservice (context, 0, startserviceinten, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibfullscreen, pfullintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

3. Send button action
1. Define an intent to send the button action
Eg:Intent previnten = new intent ("Prev ");

2. instantiate a pendingintent with intent and use the getbroadcast method of pendingintent to send broadcasts.
Eg:Pendingintent pprevintent = pendingintent. getbroadcast (this, 0, previnten, 0 );

3. instantiate remoteview, which corresponds to the corresponding widget Layout
Eg:Remoteviews views = newremoteviews (getpackagename (), R. layout. widget );

4. Set button events for buttons or imagebutton on remoteview
Eg:Views. setonclickpendingintent (R. Id. ibprev, pprevintent );

5. Update the appwidget Interface
① If the appwidget interface is updated in the onupdate () method
Eg:Appwidgetmanager. updateappwidget (appwidgetids, activityview );
② If the appwidget interface is updated outside the onupdate () method (usually within the Service), several variables need to be defined.
Eg:Public remoteviews views; // remoteview object
  Publiccomponentname thiswidget; // component name
    Public appwidgetmanager manager; // appwidget Manager

    Thiswidget = new componentname (this, pictureappwidgetprovider. Class );
    Manager = appwidgetmanager. getinstance (this );
    Manager. updateappwidget (thiswidget, views );

6. Receive the action
① Receive in appwidget's onreceive Method
Actions: Add action to manifest. xml.
Eg:<Intent-filter>
     <Actionandroid: Name = "android. appwidget. Action. appwidget_update"> </Action>
     <Actionandroid: Name = "Prev"> </Action>
   </Intent-filter>
Compile the action to be implemented in the onreceive () method
Eg:If (intent. getaction (). Equals ("Prev "))
{
  // Write the action to be implemented after receiving the action
}
② Receive in Service
Register a broadcastreceive and declare the receiver.
Eg:Intentfilter filter = new intentfilter ();
    Filter. addaction ("Prev ");
    Registerreceiver (docommand, filter );
Compile, write the action to be implemented in the onreceive method of the broadcastreceive class
Eg:If (intent. getaction (). Equals ("Prev "))
{
  // Write the action to be implemented after receiving the action
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.