天天看點

appWidget 簡單入門學習筆記1,讓桌面能夠添加你的appWidget2,小小的進階為widget實作運作activity

//一個實作AppWidgetProvider的類   

public class TomAppWidgetProvider extends  

 AppWidgetProvider {   } 

<!--放在 res/xml 檔案夾中命名為tom_appwidget_info.xml-->  

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"      

android:minWidth="294dp"      

android:minHeight="72dp"      

android:updatePeriodMillis="86400000"      

android:initialLayout="@layout/tom_appwidget"      

android:configure="kg.tom.AppWidgetConfigure"     > </appwidget-provider>   

<!-- android:initialLayout: 初始化你的appWidget布局 --> 

<!--放置在res/layout/tom_appwidget_provider.xml-->  

<?xml version="1.0" encoding="utf-8"?>     

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      

android:layout_width="294dp"      

android:layout_height="72dp"      

android:orientation="vertical"    > <TextView       

android:id="@+id/appwidget_text"      

android:layout_width="wrap_content"      

android:layout_height="wrap_content"      

android:textColor="#ff000000"/>     <Button          

android:id="@+id/appwidget_button"          

android:layout_width="wrap_content"          

android:layout_height="wrap_content"          

android:text="@android:string/ok"    />     

</LinearLayout> 

在AndroidManifest.xml中聲明你的appWidget

<receiver android:name="TomAppWidgetProvider" >       

<intent-filter>   

<!--讓系統能夠設别到你的appWidgetProvider的動作-->        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />       

</intent-filter>   

<!--設定你的appWidget的布局-->      

<meta-data android:name="android.appwidget.provider"               android:resource="@xml/tom_appwidget_info" />   

</receiver> 

長按你的Home 界面,你就會看到你的widget已經在清單當中

<a target="_blank" href="http://blog.51cto.com/attachment/201202/220533908.png"></a>

但是,現在點選是會出錯的,這時候我們需要設定我們的AppWidgetConfigure

在activity中增加一個Intent-filter

&lt;intent-filter&gt;               

&lt;!-- 讓系統能夠設别到你的appWidgetProvider的動作 --&gt;              

&lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;           

&lt;/intent-filter&gt; 

public class AppWidgetConfigure extends Activity {       

@Override      

public void onCreate(Bundle savedInstanceState) {           

super.onCreate(savedInstanceState);           

//設定widgetId           

int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;           

//1,将setResult 設定為 取消,用于取消widget host         setResult(RESULT_CANCELED);                   

//2,從Intent 中找到widget 的id            

Intent intent = getIntent();           

Bundle extras = intent.getExtras();           

if(extras != null){               

mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);         }                     

//3,如果擷取不到 appWidget id 我們就結束           

if(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){               

Log.d("app", "擷取失敗退出!!!");               

finish();         }                               

final Context context = AppWidgetConfigure.this;           

//4,執行個體化你的 appWidget           

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);                     

//5,更新widget           

Intent resultValue = new Intent();         resultValue.putExtra(appWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);           

setResult(RESULT_OK, resultValue);           

finish();                         } } 

2,然後寫provider的代碼

public class TomAppWidgetProvider extends AppWidgetProvider {       

public void onUpdate(Context context, AppWidgetManager appWidgetManager,               

int[] appWidgetIds) {           

// TODO Auto-generated method stub           

super.onUpdate(context, appWidgetManager, appWidgetIds);           

final int N = appWidgetIds.length;           

Log.d("app", "onUpdate---&gt;Ids==="+String.valueOf(N) );           

for(int i=0; i &lt; N; i++){               

int appWidgetId = appWidgetIds[i];                             

updateAppWidget(context, appWidgetManager, appWidgetId);         }               }             

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){           

Log.d("app", "update----&gt;id" + appWidgetId);           

//1,widget中的的标題           

CharSequence text = "這是我第一個widget";           

//2,widget顯示用布局,并設定text顯示的值           

RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.tom_appwidget_provider);         views.setTextViewText(R.id.appwidget_text, text);           

//3,通知widget manager 更新         appWidgetManager.updateAppWidget(appWidgetId, views);               }             

public void onDeleted(Context context, int[] appWidgetIds) {           

// TODO Auto-generated method stub         super.onDeleted(context, appWidgetIds);           

//删除的時候調用的方法           

int appids = appWidgetIds.length;           

Log.d("app", "onDelete---&gt;" + appids);     } } 

然後就可以運作了:

<a target="_blank" href="http://blog.51cto.com/attachment/201202/220540114.png"></a>

順便附上一張簡單原理圖:..

<a target="_blank" href="http://blog.51cto.com/attachment/201202/220546695.jpg"></a>

隻要在加上幾行代碼的appwidget 就能實作挑戰到Activity的功能

1,建立一個HelloAppWidget的activity

//1,設定顯示用标題           

CharSequence text = "這是我第一個widget";                     

//1.1,增加跳轉用activity相關 intent           

Intent intent = new Intent(context, HelloAppWidget.class);           

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);           

//2,如果,沒有在xml中聲明RemoteViews布局,這裡就必須要讓其他布局基于RemoteViews           

//2.1将需要跳轉的intent綁定到appWidget button中         views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent);                     

//3,通知widget manager 更新         appWidgetManager.updateAppWidget(appWidgetId, views);               } 

<a target="_blank" href="http://blog.51cto.com/attachment/201202/220553508.png"></a>

跳轉到特定activity…ps:使用activity記得在AndroidManifest.xml中注冊

繼續閱讀