android學習之AppWidget

來源:互聯網
上載者:User

android學習之AppWidget
一、 什麼是AppWidget
案頭上見到的那種一個個的小視窗,利用這個小視窗可以給使用者提供一些方便快捷的操作,有點像捷徑。。。

二、 與AppWidget相關的資料
1、AppWidgetProviderInfo對象,為appWidget提供中繼資料,包括布局、更新頻率等等,這個對象被定義在xml檔案中
2、appWidgetProvider,定義了appWidget的基本生命週期
三、建立一個AppWidget的方法
1、定義AppWidgetProviderInfo對象
在res/xml檔案夾中定義一個名為example_appwidget_info.xml的檔案
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"<?最小寬度?>
android:minHeight="72dp"<?最小高度?>
android:updatePeriodMillis="86400000"<?更新時間,單位ms?>
android:initialLayout="@layout/example_appwidget"<?appwidget初始化的布局檔案?>
/>
2、為AppWidget添加布局檔案
<TextView
android:id="@+id/widgetText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="myWidget"
/>
3、實現appWidgetProvider的方法
onUpdate:在大道指定更新時間後,或者使用者想案頭添加appwidget時會調用
onDelete:當appWidget被刪除時調用
onEnabled:當第一次建立appwidget時調用
onDisabled:當最後一個appwidget被刪除時調用
onRecieve:接收廣播 appwidget是依靠廣播機制的
4、在AndroidManifest中聲明廣播
<receiver android:name = "com.example.appwidget.appWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info"
/>
</receiver>
四、AppWidget如何使用
1、pendingIntent 用於appwidget和其他activity交換資料,appwidget和建立它的activity並不在同一個進程
2、建立pendingIntent的方法:
(1)、getActivity() //用於啟動新的activity
(2)、getBroadcast() //用於啟動新的廣播,廣播會在onReceive方法中接收
(3)、getService() //用於啟動新的服務
3、remoteViews 表示一系列的view對象,這些view對象在另一個線程
4、為remoteViews 添加監聽器的方法 :setOnClickPendingIntent
五、原始碼
1、MainActivity.java
這裡什麼都不需要操作

 
  1. public class MainActivity extends Activity
  2. {

  3. @Override
  4. protected void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. }

  9. @Override
  10. public boolean onCreateOptionsMenu(Menu menu)
  11. {
  12. // Inflate the menu; this adds items to the action bar if it is present.
  13. getMenuInflater().inflate(R.menu.main, menu);
  14. return true;
  15. }

  16. }
2、myAppWidget.java
這裡操作AppWidget
 
  1. public class appWidget extends AppWidgetProvider
  2. {
  3. //定義一個常量,用於自訂action
  4. private static final String UPDATE_ACTION = "appwidget.UPDATE_APPWIDGET";
  5. @Override
  6. public void onDeleted(Context context, int[] appWidgetIds)
  7. {
  8. // TODO Auto-generated method stub
  9. super.onDeleted(context, appWidgetIds);
  10. }

  11. @Override
  12. public void onDisabled(Context context)
  13. {
  14. // TODO Auto-generated method stub
  15. super.onDisabled(context);
  16. }

  17. @Override
  18. public void onEnabled(Context context)
  19. {
  20. // TODO Auto-generated method stub
  21. super.onEnabled(context);
  22. }

  23. @Override
  24. public void onReceive(Context context, Intent intent)
  25. {
  26. // TODO Auto-generated method stub
  27. String action = intent.getAction();
  28. if(UPDATE_ACTION.equals(action))
  29. {
  30. System.out.println(action);
  31. //得到remoteViews
  32. RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  33. //給remoteViews設定動作
  34. remoteViews.setTextViewText(R.id.widgetText, "hello");
  35. //建立AppWidgetManager
  36. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  37. //建立ComponentName
  38. ComponentName compenentName = new ComponentName(context, appWidget.class);
  39. appWidgetManager.updateAppWidget(compenentName, remoteViews);
  40. }
  41. else
  42. {
  43. super.onReceive(context, intent);
  44. }
  45. }

  46. @Override
  47. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  48. int[] appWidgetIds)
  49. {
  50. //每建立一個appwidget就會添加一個id
  51. for(int i=0; i<appWidgetIds.length; i++)
  52. {
  53. System.out.println(appWidgetIds[i]);
  54. //建立一個intent對象
  55. Intent intent = new Intent(context, targetActivity.class);
  56. //建立一個pendingIntent對象,利用getActivity建立,用於啟動新的activity
  57. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  58. //建立remoteViews
  59. RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  60. //為remoteViews綁定事件處理器,當發生click事件後執行pendingIntent
  61. //第一個參數是被綁定的控制項,第二個參數指定當事件發生時執行的pendingIntent
  62. remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn1, pendingIntent);
  63. //更新appwidget
  64. appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
  65. }


  66. for(int i=0; i<appWidgetIds.length; i++)
  67. {
  68. System.out.println(appWidgetIds[i]);
  69. //建立一個intent對象
  70. Intent intent = new Intent();
  71. //設定action,這個action需要在androidManifest中聲明
  72. intent.setAction(UPDATE_ACTION);
  73. //建立一個pendingIntent對象,利用getBroadcast建立,用於發送廣播,發送的廣播會在onReceive方法中接收
  74. PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  75. //建立remoteViews
  76. RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  77. //為remoteViews綁定事件處理器,當發生click事件後執行pendingIntent
  78. //第一個參數是被綁定的控制項,第二個參數指定當事件發生時執行的pendingIntent
  79. remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn2, pendingIntent);
  80. //更新appwidget
  81. appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
  82. }
  83. // TODO Auto-generated method stub
  84. super.onUpdate(context, appWidgetManager, appWidgetIds);

  85. }
  86. }
4、example_appwidget_info.xml
這個xml檔案並不是布局檔案,它是為AppWidget提供中繼資料
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="294dp"
  5. android:minHeight="72dp"
  6. android:updatePeriodMillis="86400000"
  7. android:initialLayout="@layout/example_appwidget"
  8. />
5、example_appwiget.xml
這是AppWidget的布局檔案
 
  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:orientation="vertical" >

  6. <TextView
  7. android:id="@+id/widgetText"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="myWidget"
  11. android:background="#000000"
  12. />
  13. <Button
  14. android:id = "@+id/appWidgetBtn1"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="test Button1"
  18. />
  19. <Button
  20. android:id = "@+id/appWidgetBtn2"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="test Button2"
  24. />
  25. </LinearLayout>
6、activity_main.xml
這是MainActivity的布局檔案
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >

  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />

  14. </RelativeLayout>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.