//res.xml.widget_small.xml
//
//
//android:initialLayout="@layout/widget_layout"
//android:minWidth="72dip"
//android:minHeight="72dip"
//android:updatePeriodMillis="3600000">
//
//res.layout.widget_layout_small.xml
//
//
//android:orientation="vertical"
//android:layout_width="wrap_content"
//android:layout_height="wrap_content"
//android:minHeight="72dip"
//android:minWidth="72dip"
//android:background="@drawable/bgblacksmall"
//android:id="@+id/SmallBase"
//>
//
//android:layout_height="wrap_content"
//android:orientation="vertical"
//android:layout_gravity="center"
//android:paddingTop="5dip"
//>
//
//android:layout_height="wrap_content"
//android:id="@+id/Month"
//android:layout_gravity="center"
//android:textStyle="bold"
//android:textSize="12sp"
//android:gravity="center"
//android:textColor="#FFFFFF"
//>
//
//android:layout_height="wrap_content"
//android:layout_gravity="center"
//android:id="@+id/Date"
//android:textStyle="bold"
//android:textSize="30sp"
//android:textColor="#FFFFFF"
//>
//
//
public class TodayDate extends AppWidgetProvider{//当我们添加AppWidget应用或自动定时更新时,AppWidgetManager会广播动作名字是"android.appwidget.action.APPWIDGET_UPDATE"
的Intent,当onReceive()方法没有被重载时,onUpdate方法会接收到这些广播的Intent。类似BroadcastReceiver类,我们可以
重载AppWidgetProvider的onReceive方法,并在其中指定想要接收的Intent
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews updateView = buildUpdate(context);
appWidgetManager.updateAppWidget(appWidgetIds, updateView);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private String[] months = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月","九月", "十月", "十一月", "十二月"};
private String[] days = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};
private RemoteViews buildUpdate(Context context){
RemoteViews updateView = null;
Time time = new Time();
time.setToNow();
String month = months[time.month] + " " + time.year;
updateView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
updateView.setTextViewText(R.id.Date, new Integer(time.monthDay).toString());
updateView.setTextViewText(R.id.Month, month);
updateView.setTextViewText(R.id.WeekDay, days[time.weekDay]);
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent intent = PendingIntent.getActivity(context, 0, launchIntent, 0);
updateView.setOnClickPendingIntent(R.id.Base, intent);
return updateView;
}
}
//public class TodayDateSmall extends AppWidgetProvider{
//public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//RemoteViews updateView = buildUpdate(context);
//appWidgetManager.updateAppWidget(appWidgetIds, updateView);
//super.onUpdate(context, appWidgetManager, appWidgetIds);
//}
//private String[] months = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月","九月", "十月", "十一月", "十二月"};
//private RemoteViews buildUpdate(Context context) {
//RemoteViews updateView = null;
//Time time = new Time();
//time.setToNow();
//String month = months[time.month];
//updateView = new RemoteViews(context.getPackageName(), R.layout.widget_layout_small);
//updateView.setTextViewText(R.id.Date, new Integer(time.monthDay).toString());
//updateView.setTextViewText(R.id.Month, month);
//Intent launchIntent = new Intent();
//launchIntent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));
//launchIntent.setAction(Intent.ACTION_MAIN);
//launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//PendingIntent intent = PendingIntent.getActivity(context, 0, launchIntent, 0);
//updateView.setOnClickPendingIntent(R.id.SmallBase, intent);
//return updateView;
//}
//}
AndroidManifest.xml
//因为使用了receiver及"adroid.appwidget.action.APPWIDGET_UPDATE"所以需要声明权限
package="com.yarin.android.TodayDate"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
android:name=".TodayDate">
android:resource="@xml/widget">
android:name=".TodayDateSmall">
android:resource="@xml/widget_small">
续:
1、上例中只是更新显示了一个界面,在Widget中同样可以通过setOnClickPendingIntent方法来监听一个事件的处理,下面代码中当我们点击了"widget_button"按钮时,会弹出Toast提示
public class widget extends AppWidgetProvider{
public void onReceive(Context context, Intent intent){
super.onReceive(context,intent);
if(intent.getAction().equals("com.android.myapp.widget.CLICK")){
Toast.makeText(context ,"It works!!", Toast.LENGTH_SHORT).show();
}
}
public void onUpdate(Context context,AppWidgetManager mgr, int[] appWidgetIds){
final int N = appWidgetIds.length;
for(int i=0; i
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
Intent clickintent = new Intent("com.android.myapp.widget.CLICK");
PendingIntent pendingIntentClick = PendingIntent.getBroadcast(context, 0,clickintent,0);
views.setOnClickPendingIntent(R.id.widget_butt,PendingIntentClick);
mrg.updateAppWidget(appWidgetId,views);
}
}
}
2、在Widget中启动一个Activity,
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.mywidget);
Intent intentActivity = new Intent(context, MusicPlayer.class);//此处启动一个音乐播放界面
PendingIntent pendingIntentActivity = PendingIntent.getActivity(context,0,intentActivity,0);
rv.setOnClickPendingIntent(R.id.button,pendingIntentActivity);
appWidgetManager.updateAppWidget(appWidgetId,rv);
context.startActivity(intentActivity);//当点击"button"按钮时启动自动Activity
3、Wdiget启动Service
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.mywidget);
Intent intentService = new Intent("Service名字");
context.startService(intentService);
appWidgetManager.updateAppWidget(appWdigetgetId,rv);