Appwidget Deep----button Event passing value

Source: Internet
Author: User

In Appwidget, ImageButton and button are supported controls, and their events can be divided into three types:
First, open activity 
Second, start Service 
Iii. Send button Action 
below begins an analysis of how it is implemented.  
First, open Activity 
  1. First define a Intent  that starts the activity;
  eg:intent intent=new Intent (This, Fullscreen.class);  
  to pass data, use the Intent.putextra () method  
  Eg:intent.putExtra ("Iscircle", iscircle);  
  2, instantiate a pendingintent with intent, invoke Pendingintent's Getacticity method to start another activity  
 ① If the intent has data, you need to set the value of the last parameter to: Flag_cancel_current 
  eg:

Pendingintent pintent=pendingintent.getactivity (this, 0,intent,pendingintent.flag_cancel_current);
② If the intent does not have data, the last parameter is set to 0
Eg:pendingintent pfullintent=pendingintent.getactivity (this, 0, fullintent, 0);
3. Instantiate the RemoteView, which corresponds to the corresponding widget layout
Eg:remoteviews views= newremoteviews (Getpackagename (), r.layout.widget);
4. Set button events on RemoteView or ImageButton
Eg:views.setOnClickPendingIntent (r.id.ibfullscreen,pfullintent);
5. Update Appwidget interface
① If the Appwidget interface is updated within the OnUpdate () method
Eg:appWidgetManager.updateAppWidget (Appwidgetids, ActivityView);
② If you are updating the Appwidget interface outside of the OnUpdate () method (typically within a service), you need to define several variables
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);
Second, open service
1. Define a intent to open the service
Eg:intent startserviceinten=newintent ("Zyf.temp.Service.START");
Note: The parameter is a service-enabled action
2, using intent to instantiate a pendingintent, using Pendingintent GetService method to start a service
Eg:pendingintent pintent=pendingintent.getservice (context, 0, Startserviceinten, 0);
3. Instantiate the RemoteView, which corresponds to the corresponding widget layout
Eg:remoteviews views= newremoteviews (Getpackagename (), r.layout.widget);
4. Set button events on RemoteView or ImageButton
Eg:views.setOnClickPendingIntent (r.id.ibfullscreen,pfullintent);
5. Update Appwidget Interface
① if the Appwidget interface is updated within the OnUpdate () method
Eg:appWidgetManager.updateAppWidget (Appwidgetids, ActivityView);
② If you are updating the Appwidget interface outside of the OnUpdate () method (typically within a service), you need to define several variables
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);
Third, send button action
1. Define a intent to send the button action
Eg:intent previnten=new Intent ("PREV");
2, using intent to instantiate a pendingintent, using Pendingintent Getbroadcast method to send the broadcast
Eg:pendingintent Pprevintent=pendingintent.getbroadcast (this, 0, Previnten, 0);
3. Instantiate the RemoteView, which corresponds to the corresponding widget layout
Eg:remoteviews views= newremoteviews (Getpackagename (), r.layout.widget);
4. Set button events on RemoteView or ImageButton
Eg:views.setOnClickPendingIntent (r.id.ibprev,pprevintent);
5. Update Appwidget interface
① If the Appwidget interface is updated within the OnUpdate () method
Eg:appWidgetManager.updateAppWidget (Appwidgetids, ActivityView);
② If you are updating the Appwidget interface outside of the OnUpdate () method (typically within a service), you need to define several variables
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
① received within Appwidget's own OnReceive method
⒈ in action, to add action in Manifest.xml
Eg: <intent-filter>
<actionandroid:name= "Android.appwidget.action.APPWIDGET_UPDATE" ></action>
<actionandroid:name= "PREV" ></action>
</intent-filter>
⒉ writing the action to be implemented within the OnReceive () method
Eg:if (Intent.getaction (). Equals ("PREV"))&NBSP;
{&NBSP;
// The action to be implemented after this action is written &NBSP;
} 
② receives   within the service;
⒈ registers a broadcastreceive, declares the receiver &NBSP;
Eg:intentfilter filter=new intentfilter (); Filter.addaction ("PREV"); Registerreceiver (DoCommand, filter); ⒉, write the action to be implemented within the OnReceive method of the Broadcastreceive class &NBSP;
Eg:if ( Intent.getaction (). Equals ("PREV")) &NBSP;
{ &NBSP;
//The action to be implemented after this action is received to }

Appwidget Deep----button Event pass-through

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.