編輯:初級開發
昨天的帶指針時鐘比較簡單,今天我們繼續android自帶widget剖析,相對於alarmclock而言music程序稍微復雜些,主要是涉及到眾多事件的處理,不過可以看出如何是和服務進行交互的。繼續按照昨天的分析步驟和過程,首先我們看下music程序中androidManifest.XML中有關widgets的定義。
<receiver android:name="MediaAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@XML/appwidget_info" />
</receiver>
下面是XML/appwidget_info的內容,裡面包含了這個widget程序的基本定義。
<appwidget-provider XMLns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dip" //最小寬度
android:minHeight="72dip" //最小高度
android:updatePeriodMillis="0" //更新頻率
android:initialLayout="@layout/album_appwidget"> //widget界面布局文件
</appwidget-provider>
整個設計還是十分清晰,這裡我們就不再做過多的贅述。
public class MediaAppWidgetProvider extends AppWidgetProvider {
public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate";
static final ComponentName THIS_APPWIDGET =
new ComponentName("com.android.music", "com.android.music.MediaAppWidgetProvider");
private static MediaAppWidgetProvider sInstance;
static synchronized MediaAppWidgetProvider getInstance() {
if (sInstance == null) {
sInstance = new MediaAppWidgetProvider();
}
return sInstance;
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
defaultAppWidget(context, appWidgetIds);
// 發送一個Intent廣播給MediaPlaybackService以便立即更新
Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD);
updateIntent.putExtra(MediaPlaybackService.CMDNAME,MediaAppWidgetProvider.CMDAPPWIDGETUPDATE);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
context.sendBroadcast(updateIntent);
}
/*
* 初始化widget默認狀態,如果服務沒有運行,我們啟動music的時候默認單擊隱藏
*/
private void defaultAppWidget(Context context, int[] appWidgetIds) {
final Resources res = context.getResources();
final RemoteViews views = new RemoteVIEws(context.getPackageName(), R.layout.album_appwidget);
views.setViewVisibility(R.id.title, VIEw.GONE);
views.setTextVIEwText(R.id.artist, res.getText(R.string.emptyplaylist));
linkButtons(context, vIEws, false /* 沒有播放*/);
pushUpdate(context, appWidgetIds, vIEws);
}
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews vIEws) {
// 更新指定的列表
final AppWidgetManager gm = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
gm.updateAppWidget(appWidgetIds, vIEws);
} else {
gm.updateAppWidget(THIS_APPWIDGET, vIEws);
}
}
/**
* Check against {@link AppWidgetManager} if there are any instances of this widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(THIS_APPWIDGET);
return (appWidgetIds.length > 0);
}
/**
* Handle a change notification coming over from {@link MediaPlaybackService}
*/
void notifyChange(MediaPlaybackService service, String what) {
if (hasInstances(service)) {
if (MediaPlaybackService.PLAYBACK_COMPLETE.equals(what) ||
MediaPlaybackService.META_CHANGED.equals(what) ||
MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)) {
performUpdate(service, null);
}
}
}
/**
* Update all active widget instances by pushing changes
*/
void performUpdate(MediaPlaybackService service, int[] appWidgetIds) {
final Resources res = service.getResources();
final RemoteViews views = new RemoteVIEws(service.getPackageName(), R.layout.album_appwidget);
final int track = service.getQueuePosition() + 1;
CharSequence titleName = service.getTrackName();
CharSequence artistName = service.getArtistName();
CharSequence errorState = null;
// Format title string with track number, or show SD card message
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_SHARED) ||
status.equals(Environment.MEDIA_UNMOUNTED)) {
errorState = res.getText(R.string.sdcard_busy_title);
} else if (status.equals(Environment.MEDIA_REMOVED)) {
errorState = res.getText(R.string.sdcard_missing_title);
} else if (titleName == null) {
errorState = res.getText(R.string.emptyplaylist);
}
if (errorState != null) {
// Show error state to user
views.setViewVisibility(R.id.title, VIEw.GONE);
views.setTextVIEwText(R.id.artist, errorState);
} else {
// No error, so show normal titles
views.setViewVisibility(R.id.title, VIEw.VISIBLE);
views.setTextVIEwText(R.id.title, titleName);
views.setTextVIEwText(R.id.artist, artistName);
}
// Set correct drawable for pause state
final boolean playing = service.isPlaying();
if (playing) {
views.setImageVIEwResource(R.id.control_play, R.drawable.appwidget_pause);
} else {
views.setImageVIEwResource(R.id.control_play, R.drawable.appwidget_play);
}
// Link actions buttons to intents
linkButtons(service, vIEws, playing);
pushUpdate(service, appWidgetIds, vIEws);
}
/**
* Link up various button actions using {@link PendingIntents}.
*
* @param playerActive True if player is active in background, which means
* widget click will launch {@link MediaPlaybackActivity},
* otherwise we launch {@link MusicBrowserActivity}.
*/
private void linkButtons(Context context, RemoteViews vIEws, boolean playerActive) {
// Connect up various buttons and touch events
Intent intent;
PendingIntent pendingIntent;
final ComponentName serviceName = new ComponentName(context, MediaPlaybackService.class);
if (playerActive) {
intent = new Intent(context, MediaPlaybackActivity.class);
pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, intent, 0 /* no flags */);
vIEws.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);
} else {
intent = new Intent(context, MusicBrowserActivity.class);
pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, intent, 0 /* no flags */);
vIEws.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);
}
intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION);
intent.setComponent(serviceName);
pendingIntent = PendingIntent.getService(context,
0 /* no requestCode */, intent, 0 /* no flags */);
vIEws.setOnClickPendingIntent(R.id.control_play, pendingIntent);
intent = new Intent(MediaPlaybackService.NEXT_ACTION);
intent.setComponent(serviceName);
pendingIntent = PendingIntent.getService(context,
0 /* no requestCode */, intent, 0 /* no flags */);
vIEws.setOnClickPendingIntent(R.id.control_next, pendingIntent);
}
}