歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之App Widgets開發初步

Android之App Widgets開發初步

日期:2017/3/1 10:25:55   编辑:Linux編程
AppWidget就是我們平常在桌面上見到的那種一個個的小窗口,利用這個小窗口可以給用戶提供一些方便快捷的操作。下面讓我們在桌面 上實現一個簡單的AppWidget。

首先定義AppWidgetProviderInfo:在res/xml文件夾中定義一個名為 :myappwidgetprovider.xml。

  1. <appwidget-provider xmlns:Android="http://schemas.android.com/apk/res/android"
  2. android:minWidth="72dp"
  3. android:minHeight="72dp"
  4. android:updatePeriodMillis="86400000"
  5. android:initialLayout="@layout/myappwidget">
  6. </appwidget-provider>
然後為App Widget指定樣式和布局:myappwidget.xml 和我們平時的布局文件一樣,可以設置自己喜歡的類型。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#ffffff"
  6. android:gravity="center"
  7. android:orientation="vertical" >
  8. <ImageView
  9. android:id="@+id/imageView1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:src="@drawable/ic_launcher" />
  13. <Button
  14. android:id="@+id/button"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Button" />
  18. </LinearLayout>
接下來實現繼承AppWidgetProvider的類:ExampleAppWidgetProvider
  1. public class ExampleAppWidgetProvider extends AppWidgetProvider {
  2. @Override
  3. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  4. int[] appWidgetIds) {
  5. // TODO Auto-generated method stub
  6. final int N = appWidgetIds.length;
  7. for (int i=0; i<N; i++) {
  8. int appWidgetId = appWidgetIds[i];
  9. Intent intent = new Intent(context, SimpleDemoActivity.class);
  10. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  11. RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myappwidget);
  12. views.setOnClickPendingIntent(R.id.button, pendingIntent);
  13. // Tell the AppWidgetManager to perform an update on the current app widget
  14. appWidgetManager.updateAppWidget(appWidgetId, views);
  15. }
  16. }
  17. @Override
  18. public void onReceive(Context context, Intent intent) {
  19. // TODO Auto-generated method stub
  20. super.onReceive(context, intent);
  21. }
  22. }
其實AppWidgetProvider就是繼承了BroadcastReceiver,可以看成是一個特殊的BroadcastReceiver。它裡面有兩個重要的方法onReceive()onUpdate()

最後在manifest中加入:

  1. <application
  2. android:icon="@drawable/ic_launcher"
  3. android:label="@string/app_name" >
  4. <activity
  5. android:label="@string/app_name"
  6. android:name=".SimpleDemoActivity" >
  7. <intent-filter >
  8. <action android:name="android.intent.action.MAIN" />
  9. <category android:name="android.intent.category.LAUNCHER" />
  10. </intent-filter>
  11. </activity>
  12. <receiver android:name="ExampleAppWidgetProvider" >
  13. <intent-filter >
  14. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  15. </intent-filter>
  16. <meta-data
  17. android:name="android.appwidget.provider"
  18. android:resource="@xml/myappwidgetprovider" />
  19. </receiver>
  20. </application>

這樣就完成了一個簡單了Widgets,運行這個工程。

看下效果:

這樣就有了我們自己的窗口小部件,選中。

這就是我們剛才布局的窗口小部件。點擊button按鈕會相應我們綁定的intent事件。

這只是一個簡單的體驗,以後會實現一個復雜的AppWidget。見 http://www.linuxidc.com/Linux/2012-03/57376.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved