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
這裡什麼都不需要操作
- public class MainActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
2、myAppWidget.java
這裡操作AppWidget
- public class appWidget extends AppWidgetProvider
- {
- //定義一個常量,用於自訂action
- private static final String UPDATE_ACTION = "appwidget.UPDATE_APPWIDGET";
- @Override
- public void onDeleted(Context context, int[] appWidgetIds)
- {
- // TODO Auto-generated method stub
- super.onDeleted(context, appWidgetIds);
- }
- @Override
- public void onDisabled(Context context)
- {
- // TODO Auto-generated method stub
- super.onDisabled(context);
- }
- @Override
- public void onEnabled(Context context)
- {
- // TODO Auto-generated method stub
- super.onEnabled(context);
- }
- @Override
- public void onReceive(Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- String action = intent.getAction();
- if(UPDATE_ACTION.equals(action))
- {
- System.out.println(action);
- //得到remoteViews
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
- //給remoteViews設定動作
- remoteViews.setTextViewText(R.id.widgetText, "hello");
- //建立AppWidgetManager
- AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
- //建立ComponentName
- ComponentName compenentName = new ComponentName(context, appWidget.class);
- appWidgetManager.updateAppWidget(compenentName, remoteViews);
- }
- else
- {
- super.onReceive(context, intent);
- }
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds)
- {
- //每建立一個appwidget就會添加一個id
- for(int i=0; i<appWidgetIds.length; i++)
- {
- System.out.println(appWidgetIds[i]);
- //建立一個intent對象
- Intent intent = new Intent(context, targetActivity.class);
- //建立一個pendingIntent對象,利用getActivity建立,用於啟動新的activity
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
- //建立remoteViews
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
- //為remoteViews綁定事件處理器,當發生click事件後執行pendingIntent
- //第一個參數是被綁定的控制項,第二個參數指定當事件發生時執行的pendingIntent
- remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn1, pendingIntent);
- //更新appwidget
- appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
- }
- for(int i=0; i<appWidgetIds.length; i++)
- {
- System.out.println(appWidgetIds[i]);
- //建立一個intent對象
- Intent intent = new Intent();
- //設定action,這個action需要在androidManifest中聲明
- intent.setAction(UPDATE_ACTION);
- //建立一個pendingIntent對象,利用getBroadcast建立,用於發送廣播,發送的廣播會在onReceive方法中接收
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
- //建立remoteViews
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
- //為remoteViews綁定事件處理器,當發生click事件後執行pendingIntent
- //第一個參數是被綁定的控制項,第二個參數指定當事件發生時執行的pendingIntent
- remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn2, pendingIntent);
- //更新appwidget
- appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
- }
- // TODO Auto-generated method stub
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
- }
4、example_appwidget_info.xml
這個xml檔案並不是布局檔案,它是為AppWidget提供中繼資料
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="294dp"
- android:minHeight="72dp"
- android:updatePeriodMillis="86400000"
- android:initialLayout="@layout/example_appwidget"
- />
5、example_appwiget.xml
這是AppWidget的布局檔案
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/widgetText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="myWidget"
- android:background="#000000"
- />
- <Button
- android:id = "@+id/appWidgetBtn1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="test Button1"
- />
- <Button
- android:id = "@+id/appWidgetBtn2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="test Button2"
- />
- </LinearLayout>
6、activity_main.xml
這是MainActivity的布局檔案
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </RelativeLayout>