成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

剖析Android Music Widget播放器

移動(dòng)開(kāi)發(fā)
Android Music Widget播放器是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)Android自帶Widget Music播放器的應(yīng)用,具體內(nèi)容來(lái)看本文詳解。

Android Music Widget播放器是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)Android自帶Widget Music播放器的應(yīng)用,具體內(nèi)容來(lái)看本文詳解。本文剖析Android自帶widget-Music,其涉及到眾多事件的處理,雖然麻煩,但可看出是如何和服務(wù)進(jìn)行交互的。

首先我們看下music程序中AndroidManifest.xml中有關(guān)widgets的定義。

下面是xml/appwidget_info的內(nèi)容,里面包含了這個(gè)widget程序的基本定義。

android:minWidth=”294dip” //最小寬度

android:minHeight=”72dip” //最小高度

android:updatePeriodMillis=”0″ //更新頻率

android:initialLayout=”@layout/album_appwidget”> //widget界面布局文件

整個(gè)設(shè)計(jì)還是十分清晰,這里我們就不再做過(guò)多的贅述。

  1. public class MediaAppWidgetProvider extends AppWidgetProvider {  
  2.  
  3. public static final String CMDAPPWIDGETUPDATE = “appwidgetupdate”;  
  4.  
  5. static final ComponentName THIS_APPWIDGET =  
  6. new ComponentName(”com.android.music”, “com.android.music.MediaAppWidgetProvider”);  
  7.  
  8. private static MediaAppWidgetProvider sInstance;  
  9.  
  10. static synchronized MediaAppWidgetProvider getInstance() {  
  11. if (sInstance == null) {  
  12. sInstance = new MediaAppWidgetProvider();  
  13. }  
  14. return sInstance;  
  15. }  
  16.  
  17. @Override  
  18. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
  19. defaultAppWidget(context, appWidgetIds);  
  20.  
  21. // 發(fā)送一個(gè)Intent廣播給MediaPlaybackService以便立即更新  
  22.  
  23. Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD);  
  24. updateIntent.putExtra(MediaPlaybackService.CMDNAME,MediaAppWidgetProvider.CMDAPPWIDGETUPDATE);  
  25. updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);  
  26. updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);  
  27. context.sendBroadcast(updateIntent);  
  28. }  
  29.  
  30. /*  
  31. * 初始化widget默認(rèn)狀態(tài),如果服務(wù)沒(méi)有運(yùn)行,我們啟動(dòng)music的時(shí)候默認(rèn)單擊隱藏  
  32. */  
  33. private void defaultAppWidget(Context context, int[] appWidgetIds) {  
  34. final Resources res = context.getResources();  
  35. final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.album_appwidget);  
  36.  
  37. views.setViewVisibility(R.id.title, View.GONE);  
  38. views.setTextViewText(R.id.artist, res.getText(R.string.emptyplaylist));  
  39.  
  40. linkButtons(context, views, false /* 沒(méi)有播放*/);  
  41. pushUpdate(context, appWidgetIds, views);  
  42. }  
  43.  
  44. private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {  
  45. // 更新指定的列表  
  46.  
  47. final AppWidgetManager gm = AppWidgetManager.getInstance(context);  
  48. if (appWidgetIds != null) {  
  49. gm.updateAppWidget(appWidgetIds, views);  
  50. } else {  
  51. gm.updateAppWidget(THIS_APPWIDGET, views);  
  52. }  
  53. }  
  54.  
  55. /**  
  56. * Check against {@link AppWidgetManager} if there are any instances of this widget.  
  57. */  
  58. private boolean hasInstances(Context context) {  
  59. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
  60. int[] appWidgetIds = appWidgetManager.getAppWidgetIds(THIS_APPWIDGET);  
  61. return (appWidgetIds.length > 0);  
  62. }  
  63.  
  64. /**  
  65. * Handle a change notification coming over from {@link MediaPlaybackService}  
  66. */  
  67. void notifyChange(MediaPlaybackService service, String what) {  
  68. if (hasInstances(service)) {  
  69. if (MediaPlaybackService.PLAYBACK_COMPLETE.equals(what) ||  
  70. MediaPlaybackService.META_CHANGED.equals(what) ||  
  71. MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)) {  
  72. performUpdate(service, null);  
  73. }  
  74. }  
  75. }  
  76.  
  77. /**  
  78. * Update all active widget instances by pushing changes  
  79. */  
  80. void performUpdate(MediaPlaybackService service, int[] appWidgetIds) {  
  81. final Resources res = service.getResources();  
  82. final RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.album_appwidget);  
  83.  
  84. final int track = service.getQueuePosition() + 1;  
  85. CharSequence titleName = service.getTrackName();  
  86. CharSequence artistName = service.getArtistName();  
  87. CharSequence errorState = null;  
  88.  
  89. // Format title string with track number, or show SD card message  
  90. String status = Environment.getExternalStorageState();  
  91. if (status.equals(Environment.MEDIA_SHARED) ||  
  92. status.equals(Environment.MEDIA_UNMOUNTED)) {  
  93. errorState = res.getText(R.string.sdcard_busy_title);  
  94. } else if (status.equals(Environment.MEDIA_REMOVED)) {  
  95. errorState = res.getText(R.string.sdcard_missing_title);  
  96. } else if (titleName == null) {  
  97. errorState = res.getText(R.string.emptyplaylist);  
  98. }  
  99.  
  100. if (errorState != null) {  
  101. // Show error state to user  
  102. views.setViewVisibility(R.id.title, View.GONE);  
  103. views.setTextViewText(R.id.artist, errorState);  
  104.  
  105. } else {  
  106. // No error, so show normal titles  
  107. views.setViewVisibility(R.id.title, View.VISIBLE);  
  108. views.setTextViewText(R.id.title, titleName);  
  109. views.setTextViewText(R.id.artist, artistName);  
  110. }  
  111.  
  112. // Set correct drawable for pause state  
  113. final boolean playing = service.isPlaying();  
  114. if (playing) {  
  115. views.setImageViewResource(R.id.control_play, R.drawable.appwidget_pause);  
  116. } else {  
  117. views.setImageViewResource(R.id.control_play, R.drawable.appwidget_play);  
  118. }  
  119.  
  120. // Link actions buttons to intents  
  121. linkButtons(service, views, playing);  
  122.  
  123. pushUpdate(service, appWidgetIds, views);  
  124. }  
  125.  
  126. /**  
  127. * Link up various button actions using {@link PendingIntents}.  
  128. *  
  129. * @param playerActive True if player is active in background, which means  
  130. * widget click will launch {@link MediaPlaybackActivity},  
  131. * otherwise we launch {@link MusicBrowserActivity}.  
  132. */  
  133. private void linkButtons(Context context, RemoteViews views, boolean playerActive) {  
  134. // Connect up various buttons and touch events  
  135. Intent intent;  
  136. PendingIntent pendingIntent;  
  137.  
  138. final ComponentName serviceName = new ComponentName(context, MediaPlaybackService.class);  
  139.  
  140. if (playerActive) {  
  141. intent = new Intent(context, MediaPlaybackActivity.class);  
  142. pendingIntent = PendingIntent.getActivity(context,  
  143. 0 /* no requestCode */, intent, 0 /* no flags */);  
  144. views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);  
  145. } else {  
  146. intent = new Intent(context, MusicBrowserActivity.class);  
  147. pendingIntent = PendingIntent.getActivity(context,  
  148. 0 /* no requestCode */, intent, 0 /* no flags */);  
  149. views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);  
  150. }  
  151.  
  152. intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION);  
  153. intent.setComponent(serviceName);  
  154. pendingIntent = PendingIntent.getService(context,  
  155. 0 /* no requestCode */, intent, 0 /* no flags */);  
  156. views.setOnClickPendingIntent(R.id.control_play, pendingIntent);  
  157.  
  158. intent = new Intent(MediaPlaybackService.NEXT_ACTION);  
  159. intent.setComponent(serviceName);  
  160. pendingIntent = PendingIntent.getService(context,  
  161. 0 /* no requestCode */, intent, 0 /* no flags */);  
  162. views.setOnClickPendingIntent(R.id.control_next, pendingIntent);  
  163. }  

小結(jié):剖析Android Music Widget播放器的內(nèi)容介紹完了,希望通過(guò)Android Music Widget內(nèi)容的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-09-08 13:36:26

Android Wid播放器

2011-06-13 09:33:04

2022-08-16 17:37:06

視頻播放器鴻蒙

2015-01-22 15:44:55

Android源碼音樂(lè)播放器

2010-10-26 09:00:48

Winamp應(yīng)用

2011-06-27 11:23:21

Qt 音樂(lè)播放器

2011-09-08 14:50:51

JavascriptWidget

2017-03-01 14:01:31

android多媒體音樂(lè)代碼

2011-04-06 10:03:45

谷歌云計(jì)算Android音

2011-07-20 16:21:20

iPhone 視頻 播放器

2010-07-30 09:35:47

Flex播放器

2015-05-21 15:25:42

VLC播放器

2011-09-05 18:08:01

MTK音頻播放器

2014-12-31 16:52:53

音樂(lè)播放器源碼

2012-05-03 09:51:09

HTML5

2010-06-11 12:53:56

openSUSE播放器

2022-06-21 14:41:38

播放器適配西瓜視頻

2009-12-17 15:10:31

Linux音樂(lè)播放器

2018-05-25 14:37:58

2011-03-14 09:55:25

AndroidWidget
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: av中文字幕在线 | 欧美日韩在线一区二区 | 亚洲日本欧美日韩高观看 | 免费成人av网站 | 久久精品亚洲国产奇米99 | 国产精品美女久久久av超清 | 日韩精品久久一区二区三区 | 伊人色综合久久久天天蜜桃 | 国产精品免费在线 | 亚洲一区二区免费视频 | 国产欧美一区二区精品忘忧草 | 夜夜爽夜夜操 | 日本免费一区二区三区四区 | 久久精品国产99国产精品 | 亚洲欧洲一区二区 | 国产福利91精品 | 日韩在线中文字幕 | 国产黄色在线观看 | 99re热精品视频 | 国产精品一区二区久久 | 男人av在线| 91精品国产色综合久久 | 日韩中文字幕免费在线 | 99综合| 久久精品久久久久久 | 狠狠操天天干 | 日韩影院一区 | 日本成人综合 | www.午夜| 国产精品久久久久久久久久 | 色婷婷婷婷色 | 欧美一级片久久 | 成人在线播放网站 | 一区二区在线免费观看 | av在线成人 | 中文字幕日韩欧美 | cao视频| 亚洲欧美一区二区三区国产精品 | 国产精品久久777777 | 欧美久久久久久久 | 三级视频在线观看 |