Android Widget 小部件(三) 在Activity中添加Widget

[java] view plaincopyprint?
  1. package com.jackie.ui;  
  2.   
  3. import static android.util.Log.d;  
  4. import android.app.Activity;  
  5. import android.appwidget.AppWidgetHost;  
  6. import android.appwidget.AppWidgetManager;  
  7. import android.appwidget.AppWidgetProviderInfo;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnLongClickListener;  
  12. import android.widget.FrameLayout;  
  13. import android.widget.FrameLayout.LayoutParams;  
  14.   
  15. /** 
  16.  * 在activity中,长按,弹出widget选择界面, 
  17.  * 选择的widget有配置就先弹出, 
  18.  * 创建widget的view 将view加载到 activity中 
  19.  */  
  20. public class AddWidgetActivity extends Activity {  
  21.     private AppWidgetHost mAppWidgetHost;  
  22.     private AppWidgetManager mAppWidgetManager;  
  23.     private FrameLayout frameLayout;  
  24.     private static final int REQUEST_PICK_APPWIDGET = 1;  
  25.     private static final int REQUEST_CREATE_APPWIDGET = 2;    
  26.     private static final int APPWIDGET_HOST_ID = 0x100;     //用于标识  
  27.     private static final String EXTRA_CUSTOM_WIDGET = "custom_widget";  
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.           
  33.         mAppWidgetManager = AppWidgetManager.getInstance(getApplicationContext());  
  34.         mAppWidgetHost = new AppWidgetHost(getApplicationContext(), APPWIDGET_HOST_ID);  
  35.         //开始监听widget的变化  
  36.         mAppWidgetHost.startListening();  
  37.           
  38.         frameLayout = new FrameLayout(this);  
  39.         frameLayout.setOnLongClickListener(new OnLongClickListener() {  
  40.               
  41.             @Override  
  42.             public boolean onLongClick(View v) {  
  43.                 showWidgetChooser();  
  44.                 return true;  
  45.             }  
  46.         });  
  47.         setContentView(frameLayout);  
  48.     }  
  49.       
  50.     @Override  
  51.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  52.         if (resultCode == RESULT_OK) {  
  53.             switch (requestCode) {  
  54.             case REQUEST_PICK_APPWIDGET:  
  55.                 addAppWidget(data);  
  56.                 break;  
  57.             case REQUEST_CREATE_APPWIDGET:  
  58.                 completeAddAppWidget(data);  
  59.                 break;  
  60.             }  
  61.         } else if (requestCode == REQUEST_PICK_APPWIDGET &&  
  62.                 resultCode == RESULT_CANCELED && data != null) {  
  63.             // Clean up the appWidgetId if we canceled  
  64.             int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);  
  65.             if (appWidgetId != -1) {  
  66.                 mAppWidgetHost.deleteAppWidgetId(appWidgetId);  
  67.             }  
  68.         }  
  69.     }  
  70.       
  71.     /** 
  72.      * 选中了某个widget之后,根据是否有配置来决定直接添加还是弹出配置activity 
  73.      * @param data 
  74.      */  
  75.     private void addAppWidget(Intent data) {  
  76.         int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);  
  77.   
  78.         String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);  
  79.         d("addAppWidget""data:"+ customWidget);  
  80.         if ("search_widget".equals(customWidget)) {  
  81.             //这里直接将search_widget删掉了  
  82.             mAppWidgetHost.deleteAppWidgetId(appWidgetId);  
  83.         } else {  
  84.             AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);  
  85.               
  86.             d("addAppWidget""configure:"+ appWidget.configure);  
  87.             if (appWidget.configure != null) {  
  88.                 //有配置,弹出配置  
  89.                 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);  
  90.                 intent.setComponent(appWidget.configure);  
  91.                 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  92.   
  93.                 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);  
  94.             } else {  
  95.                 //没有配置,直接添加  
  96.                 completeAddAppWidget(data);  
  97.             }  
  98.         }  
  99.     }  
  100.       
  101.     /** 
  102.      * 请求添加一个新的widget:用于选取系统中的widget 
  103.      */  
  104.     private void showWidgetChooser() {  
  105.         int appWidgetId = mAppWidgetHost.allocateAppWidgetId();  
  106.         Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);  
  107.         pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  108.         startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);  
  109.     }      
  110.       
  111.     /** 
  112.      * 添加widget 
  113.      * @param data 
  114.      */  
  115.     private void completeAddAppWidget(Intent data) {  
  116.         Bundle extras = data.getExtras();  
  117.         int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);  
  118.   
  119.         d("completeAddAppWidget""dumping extras content="+extras.toString());  
  120.         d("completeAddAppWidget""appWidgetId:"+ appWidgetId);  
  121.         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);  
  122.           
  123.         View hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);  
  124.         frameLayout.setLayoutParams(new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, appWidgetInfo.minHeight));  
  125.         frameLayout.addView(hostView);   
  126.     }  
  127.   
  128. }  

你可能感兴趣的:(widget)