Android widgets (3) add widgets and androidwidgets to the Activity

Source: Internet
Author: User

Android widgets (3) add widgets and androidwidgets to the Activity


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. frameLayou T. layoutParams;/*** in the activity, long press to bring up the widget selection interface. * If the selected widget has configuration, it will pop up first, * Create a widget view and load the view to the activity */public class AddWidgetActivity extends Activity {private AppWidgetHost mAppWidgetHost; private AppWidgetManager mAppWidgetManager; private FrameLayout frameLayout; private static final int Limit = 1; private static final int REQUEST_CREATE_APPWIDGET = 2; private static final int AP PWIDGET_HOST_ID = 0x100; // identifies private static 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 listening for widget changes mAppWidgetHost. startListening (); fra MeLayout = 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 & data! = 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 is selected, depending on whether the configuration is available, you can directly add or pop up the configuration activity * @ param data */private void addAppWidget (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) {// directly here Searchwidgethost is deleted from search_widget. deleteAppWidgetId (appWidgetId);} else {AppWidgetProviderInfo appWidget = mAppWidgetManager. getAppWidgetInfo (appWidgetId); d ("addAppWidget", "configure:" + appWidget. configure); if (appWidget. configure! = Null) {// configured. Intent intent = new Intent (AppWidgetManager) is displayed. ACTION_APPWIDGET_CONFIGURE); intent. setComponent (appWidget. configure); intent. putExtra (AppWidgetManager. EXTRA_APPWIDGET_ID, appWidgetId); startActivityForResult (intent, REQUEST_CREATE_APPWIDGET);} else {// No configuration, directly add completeAddAppWidget (data );}}} /*** request to add a new widget: used to select the widget in the system */private void showWidgetChooser () {int appWidgetId = 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 );}}


Refer:

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


An issue with using android Tabwidget

VIEWGROUP

What is the role of the android inventory file? In which lifecycle phase of the activity is the relative completion of widgets implemented?

This question is too professional! No one knows about Baidu. I suggest you go to a foreign forum to ask questions !!!

Related Article

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.