Android Widget Widgets (iii) Add widgets to activity

Source: Internet
Author: User


Package Com.stone.ui;import static Android.util.log.d;import Android.app.activity;import Android.appwidget.appwidgethost;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetproviderinfo;import Android.content.intent;import Android.os.Bundle;import Android.view.view;import Android.view.view.onlongclicklistener;import Android.widget.framelayout;import android.widget.framelayout.layoutparams;/** * In activity, long press, Popup widget Selection interface, * Selected widget is configured to pop up first, * Create widget View Load view into Activity */public class Addwidgetactivity extends activity {private Appwidgethost Mappwidgethost;private Appwidgetmanager mappwidgetmanager;private framelayout framelayout;private static final int request_pick_appwidget = 1; private static final int request_create_appwidget = 2;private static final int appwidget_host_id = 0x100;//used to identify private STA    Tic final String extra_custom_widget = "Custom_widget"; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancesTate);        Mappwidgetmanager = Appwidgetmanager.getinstance (Getapplicationcontext ());        Mappwidgethost = new Appwidgethost (Getapplicationcontext (), appwidget_host_id);                Start monitoring widget changes mappwidgethost.startlistening ();        Framelayout = new Framelayout (this); Framelayout.setonlongclicklistener (New Onlongclicklistener () {@Overridepublic Boolean onlongclick (View v) {        Showwidgetchooser (); return true;});    Setcontentview (framelayout);  } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = =                RESULT_OK) {switch (requestcode) {case request_pick_appwidget:addappwidget (data);            Break                Case Request_create_appwidget:completeaddappwidget (data);    Break }} else if (Requestcode = = Request_pick_appwidget && ResultCode = = result_canceled && D ATA! = NULL) {// Clean up the Appwidgetid if we canceled int appwidgetid = Data.getintextra (appwidgetmanager.extra_appwidget_id            ,-1);            if (Appwidgetid! =-1) {Mappwidgethost.deleteappwidgetid (Appwidgetid); }}}/** * After a widget has been selected, depending on whether the configuration is configured to directly add or eject configuration activity * @param data */private void Addap        Pwidget (Intent data) {int appwidgetid = Data.getintextra (appwidgetmanager.extra_appwidget_id,-1);        String customwidget = Data.getstringextra (extra_custom_widget);        D ("Addappwidget", "Data:" + customwidget); if ("Search_widget". Equals (Customwidget)) {//Here the Search_widget is deleted directly Mappwidgethost.deleteappwidgetid (app        Widgetid);                        } else {Appwidgetproviderinfo appwidget = Mappwidgetmanager.getappwidgetinfo (Appwidgetid);            D ("Addappwidget", "Configure:" + appwidget.configure); if (appwidget.configure! = null) {//has configuration, popup config Intent intent = new Intent (appwidgetmanager.action_appwidget_configure);                Intent.setcomponent (appwidget.configure);                Intent.putextra (appwidgetmanager.extra_appwidget_id, Appwidgetid);            Startactivityforresult (Intent, request_create_appwidget);            } else {//is not configured to add Completeaddappwidget (data) directly; }}}/** * Request to add a new widget: Used to select the widget in the system */private void Showwidgetchooser () {int APPWIDG        Etid = Mappwidgethost.allocateappwidgetid ();        Intent pickintent = new Intent (Appwidgetmanager.action_appwidget_pick);        Pickintent.putextra (appwidgetmanager.extra_appwidget_id, Appwidgetid);    Startactivityforresult (Pickintent, request_pick_appwidget);  }/** * Add widget * @param data */private void Completeaddappwidget (Intent data) {Bundle        Extras = Data.getextras (); int appwidgetid = Extras.getint (appwidgetmanager.extra_appwidget_id,-1);       D ("Completeaddappwidget", "dumping extras content=" +extras.tostring ());        D ("Completeaddappwidget", "Appwidgetid:" + Appwidgetid);                Appwidgetproviderinfo appwidgetinfo = Mappwidgetmanager.getappwidgetinfo (Appwidgetid);        View Hostview = Mappwidgethost.createview (this, Appwidgetid, appwidgetinfo);        Framelayout.setlayoutparams (New Layoutparams (FrameLayout.LayoutParams.MATCH_PARENT, appwidgetinfo.minheight));     Framelayout.addview (Hostview); }}


Reference:

http://chroya.iteye.com/blog/825407

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.