編輯:關於Android編程
實現代碼如下:
package com.iwanghang.drmplayer; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.TextView; import com.iwanghang.drmplayer.adapter.MyMusicListAdapter; import com.iwanghang.drmplayer.utils.Constant; import com.iwanghang.drmplayer.vo.Mp3Info; import com.lidroid.xutils.db.sqlite.Selector; import com.lidroid.xutils.exception.DbException; import java.util.ArrayList; import java.util.List; /** * Created by iwanghang on 16/4/27. * 最近播放的音樂列表界面 */ public class MyRecordMusicActivity extends BaseActivity implements OnItemClickListener { private ListView listView_record; private TextView textView2_no_data; private DRMPlayerApp app; private ArrayListrecordMp3Infos; private MyMusicListAdapter adapter; //private boolean isChange = false;//表示當前播放列表是否為收藏列表 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = (DRMPlayerApp) getApplication(); setContentView(R.layout.activity_my_record_music_list);//綁定布局 listView_record = (ListView) findViewById(R.id.listView_record);//實例化布局 textView2_no_data = (TextView) findViewById(R.id.textView2_no_data); listView_record.setOnItemClickListener(this); initData();//初始化數據 //listView_love.setAdapter(); } private void initData() {//初始化數據 try { List list = app.dbUtils.findAll(Selector.from(Mp3Info.class).where("playTime","!=","0").orderBy("playTime",true).limit(Constant.MY_RECORD_NUM));//查找5首最近播放的音樂 if (list==null || list.size()==0){ textView2_no_data.setVisibility(View.VISIBLE);//溫馨提示.顯示 listView_record.setVisibility(View.GONE);//最近播放列表.隱藏 }else { textView2_no_data.setVisibility(View.GONE);//溫馨提示.隱藏 listView_record.setVisibility(View.VISIBLE);//最近播放列表.顯示 recordMp3Infos = (ArrayList ) list; adapter = new MyMusicListAdapter(this,recordMp3Infos); listView_record.setAdapter(adapter); } } catch (DbException e) { e.printStackTrace(); } } //把播放服務的綁定和解綁放在onResume,onPause裡,好處是,每次回到當前Activity就獲取一次播放狀態 @Override protected void onResume() { super.onResume(); bindPlayService();//綁定服務 } @Override protected void onPause() { super.onPause(); unbindPlayService();//解綁服務 } @Override protected void onDestroy() { super.onDestroy(); unbindPlayService();//解綁服務 } @Override public void publish(int progress) { } @Override public void change(int progress) { } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { //如果當前播放列表不是收藏列表 if (playService.getChangePlayList() != PlayService.RECORD_MUSIC_LIST){ playService.setMp3Infos(recordMp3Infos);//播放列表切換為最近播放 playService.setChangePlayList(PlayService.RECORD_MUSIC_LIST); System.out.println("播放列表切換為最近播放"); } playService.play(position); } }
package com.iwanghang.drmplayer; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.net.Uri; import android.os.Binder; import android.os.IBinder; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 音樂播放的服務組件 * 實現功能: * 播放 * 暫停 * 下一首 * 上一首 * 獲取當前歌曲的播放進度 *
* 需要在AndroidManifest.xml添加以下代碼: *
* 實現功能(播放模式play_mode): * 順序播放 * 隨機播放 * 單曲循環 */ public class PlayService extends Service implements OnCompletionListener, OnErrorListener { private MediaPlayer mPlayer; private int currentPosition;//當前正在播放的歌曲的位置 ArrayList
package com.iwanghang.drmplayer; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; import com.iwanghang.drmplayer.utils.ImageUtils; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import com.lidroid.xutils.db.sqlite.Selector; import com.lidroid.xutils.exception.DbException; import java.util.ArrayList; import static com.iwanghang.drmplayer.PlayService.ORDER_PLAY; import static com.iwanghang.drmplayer.PlayService.RANDOM_PLAY; import static com.iwanghang.drmplayer.PlayService.SINGLE_PLAY; /** * PlayActivity 點擊MyMusicListFragment(本地音樂)底部UI中的專輯封面圖片打開的Activity */ public class PlayActivity extends BaseActivity implements OnClickListener, OnSeekBarChangeListener { private TextView textView1_title;//歌名 private ImageView imageView1_ablum;//專輯封面圖片 private SeekBar seekBar1;//進度條 private TextView textView1_start_time, textView1_end_time;//開始時間,結束時間 private ImageView imageView1_play_mode;//菜單 private ImageView imageView3_previous, imageView2_play_pause, imageView1_next;//上一首,播放暫停,下一首 private ImageView imageView1_ablum_reflection;//專輯封面圖片倒影 private ImageView imageView1_favorite;//收藏按鈕 //private ArrayListmp3Infos; //private int position;//當前播放的位置 private boolean isPause = false;//當前播放的是否為暫停狀態 private static final int UPDATE_TIME = 0x1;//更新播放事件的標記 private DRMPlayerApp app;//取出全局對象 方便調用 // private ViewPager viewPager; // private MyPagerAdapter adapter; // private MyMusicListFragment myMusicListFragment; // private NetMusicListFragment netMusicListFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music_play); //取出全局對象 方便調用 app = (DRMPlayerApp) getApplication(); //初始化界面信息 textView1_title = (TextView) findViewById(R.id.textView1_title);//歌名 imageView1_ablum = (ImageView) findViewById(R.id.imageView1_ablum);//專輯封面圖片 seekBar1 = (SeekBar) findViewById(R.id.seekBar1);//進度條 textView1_start_time = (TextView) findViewById(R.id.textView1_start_time);//開始時間 textView1_end_time = (TextView) findViewById(R.id.textView1_end_time);//結束時間 imageView1_play_mode = (ImageView) findViewById(R.id.imageView1_play_mode);//菜單 imageView3_previous = (ImageView) findViewById(R.id.imageView3_previous);//上一首 imageView2_play_pause = (ImageView) findViewById(R.id.imageView2_play_pause);//播放暫停 imageView1_next = (ImageView) findViewById(R.id.imageView1_next);//下一首 imageView1_ablum_reflection = (ImageView) findViewById(R.id.imageView1_ablum_reflection);//專輯封面圖片倒影 imageView1_favorite = (ImageView) findViewById(R.id.imageView1_favorite);//收藏按鈕 //注冊按鈕點擊監聽事件 imageView1_play_mode.setOnClickListener(this); imageView2_play_pause.setOnClickListener(this); imageView3_previous.setOnClickListener(this); imageView1_next.setOnClickListener(this); seekBar1.setOnSeekBarChangeListener(this); imageView1_favorite.setOnClickListener(this); //mp3Infos = MediaUtils.getMp3Infos(this); //bindPlayService();//綁定服務,異步過程,綁定後需要取得相應的值,來更新界面 myHandler = new MyHandler(this); //以下直接調用change()是不行的,因為異步問題,會發生NullPointerException空指針異常 //從MyMusicListFragment的專輯封面圖片點擊時間傳過來的position(當前播放的位置) //position = getIntent().getIntExtra("position",0); //change(position); //通過在BaseActivity中綁定Service,添加如下代碼實現change() //musicUpdatrListener.onChange(playService.getCurrentProgeress()); //從MyMusicListFragment的專輯封面圖片點擊時間傳過來的isPause(當前播放的是否為暫停狀態) //isPause = getIntent().getBooleanExtra("isPause",false); } // private void initViewPager(){//專輯封面圖片Pager與歌詞Pager // View album_image_layout = getLayoutInflater().inflate(R.layout.album_image_layout,null); // //初始化界面信息 // textView1_title = (TextView) findViewById(R.id.textView1_title);//歌名 // imageView1_ablum = (ImageView) findViewById(R.id.imageView1_ablum);//專輯封面圖片 // views.add(album_image_layout);//添加專輯封面圖片Pager // views.add(getLayoutInflater().inflate(R.layout.lrc_layout));//添加歌詞Pager // viewPager.setAdapter(new MyPagerAdapter()); // } //把播放服務的綁定和解綁放在onResume,onPause裡,好處是,每次回到當前Activity就獲取一次播放狀態 @Override protected void onResume() { super.onResume(); bindPlayService();//綁定服務 } @Override protected void onPause() { super.onPause(); unbindPlayService();//解綁服務 } @Override protected void onDestroy() { super.onDestroy(); unbindPlayService();//解綁服務 } //Handler用於更新已經播放時間 private static MyHandler myHandler; //進度條改變 (fromUser 是否來自用戶的改變 , 而不是程序自身控制的改變) @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { //playService.pause();//暫停 playService.seekTo(progress);//尋找指定的時間位置 (跳到某個時間點進行播放) //playService.start();//播放 } } //進度條開始觸摸 @Override public void onStartTrackingTouch(SeekBar seekBar) { } //進度條停止觸摸 @Override public void onStopTrackingTouch(SeekBar seekBar) { } static class MyHandler extends Handler { private PlayActivity playActivity; public MyHandler(PlayActivity playActivity) { this.playActivity = playActivity; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (playActivity != null) { switch (msg.what) { case UPDATE_TIME://更新時間(已經播放時間) playActivity.textView1_start_time.setText(MediaUtils.formatTime(msg.arg1)); break; } } } } @Override public void publish(int progress) { //以下是是直接調用線程,但是不能這樣做,會報錯,線程異常 //textView1_start_time.setText(MediaUtils.formatTime(progress)); //所以,我們需要使用Handler Message msg = myHandler.obtainMessage(UPDATE_TIME);//用於更新已經播放時間 msg.arg1 = progress;//用於更新已經播放時間 myHandler.sendMessage(msg);//用於更新已經播放時間 seekBar1.setProgress(progress); } @Override public void change(int position) {//初始化,獨立播放界面的歌曲切換後的初始化界面上的歌曲信息 //if (this.playService.isPlaying()) {//獲取是否為播放狀態 System.out.println("PlayActivity #100 position = " + position); Mp3Info mp3Info = playService.mp3Infos.get(position); textView1_title.setText(mp3Info.getTitle());//設置歌名 //獲取專輯封面圖片 Bitmap albumBitmap = MediaUtils.getArtwork(this, mp3Info.getId(), mp3Info.getAlbumId(), true, false); //改變播放界面專輯封面圖片 imageView1_ablum.setImageBitmap(albumBitmap); textView1_end_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));//設置結束時間 imageView2_play_pause.setImageResource(R.mipmap.app_music_pause);//設置暫停圖片 seekBar1.setProgress(0);//設置當前進度為0 seekBar1.setMax((int) mp3Info.getDuration());//設置進度條最大值為MP3總時間 if (playService.isPlaying()) { imageView2_play_pause.setImageResource(R.mipmap.app_music_pause); } else { imageView2_play_pause.setImageResource(R.mipmap.app_music_play); } if (imageView1_ablum != null) { imageView1_ablum_reflection.setImageBitmap(ImageUtils.createReflectionBitmapForSingle(albumBitmap));//顯示倒影 } switch (playService.getPlay_mode()) { case ORDER_PLAY://順序播放 imageView1_play_mode.setImageResource(R.mipmap.app_mode_order); //imageView2_play_pause.setTag(ORDER_PLAY); break; case PlayService.RANDOM_PLAY://隨機播放 imageView1_play_mode.setImageResource(R.mipmap.app_mode_random); //imageView2_play_pause.setTag(RANDOM_PLAY); break; case PlayService.SINGLE_PLAY://單曲循環 imageView1_play_mode.setImageResource(R.mipmap.app_mode_single); //imageView2_play_pause.setTag(SINGLE_PLAY); break; default: break; } //初始化收藏狀態 try { Mp3Info loveMp3Info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId", "=", mp3Info.getMp3InfoId()));//查出歌曲,SQL語句 System.out.println("初始化收藏狀態" + loveMp3Info); // if (loveMp3Info != null) { // imageView1_favorite.setImageResource(R.mipmap.app_love_selected); // } else { // imageView1_favorite.setImageResource(R.mipmap.app_love_unselected); // } if (loveMp3Info != null) { System.out.println("loveMp3Info.getIsLove() = " + loveMp3Info.getIsLove()); if (loveMp3Info.getIsLove() == 0) {//返回值不為null,且,isLove為0時,也顯示為'未收藏' imageView1_favorite.setImageResource(R.mipmap.app_love_unselected); }else {//返回值為null,且,isLove為1時,一定顯示為'已收藏' imageView1_favorite.setImageResource(R.mipmap.app_love_selected); } } else {//返回值為null,一定顯示為'未收藏' imageView1_favorite.setImageResource(R.mipmap.app_love_unselected); } } catch (DbException e) { e.printStackTrace(); } } //如果是本地音樂,id就是id;如果是收藏音樂,id則是mp3InfoId //提供給 收藏按鈕 點擊事件時 調用. private long getId(Mp3Info mp3Info){ //初始收藏狀態 long id = 0; switch (playService.getChangePlayList()){ case PlayService.MY_MUSIC_LIST: System.out.println("當前為本地列表"); id = mp3Info.getId(); System.out.println("id =" + id); break; case PlayService.LOVE_MUSIC_LIST: System.out.println("當前為收藏列表"); id = mp3Info.getMp3InfoId(); System.out.println("id =" + id); break; default: break; } return id; } //點擊事件 @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageView2_play_pause: {//播放暫停按鈕 if (playService.isPlaying()) {//如果是播放狀態 imageView2_play_pause.setImageResource(R.mipmap.app_music_play);//設置播放圖片 playService.pause(); } else { if (playService.isPause()) { imageView2_play_pause.setImageResource(R.mipmap.app_music_pause);//設置暫停圖片 playService.start();//播放事件 } else { //只有打開APP沒有點擊任何歌曲,同時,直接點擊暫停播放按鈕時.才會調用 //playService.play(0); //只有打開APP沒有點擊任何歌曲,同時,直接點擊暫停播放按鈕時.才會調用 //默認播放的是,在PlayService的onCreate中 恢復狀態值 playService.play(playService.getCurrentPosition()); } } break; } case R.id.imageView1_next: {//下一首按鈕 playService.next();//下一首 break; } case R.id.imageView3_previous: {//上一首按鈕 playService.prev();//上一首 break; } case R.id.imageView1_play_mode: {//循環模式按鈕 //int mode = (int) imageView1_play_mode.getTag(); /** * 以下Tosat內容,在strings.xml裡,添加對應代碼 * 順序播放 *隨機播放 *單曲循環 */ switch (playService.getPlay_mode()) { case ORDER_PLAY: imageView1_play_mode.setImageResource(R.mipmap.app_mode_random); //imageView1_play_mode.setTag(RANDOM_PLAY); playService.setPlay_mode(RANDOM_PLAY); //Toast.makeText(getApplicationContext(),"隨機播放",Toast.LENGTH_SHORT).show();//這句也可以 //Toast.makeText(PlayActivity.this, "隨機播放", Toast.LENGTH_SHORT).show();//這句也可以 Toast.makeText(PlayActivity.this, getString(R.string.random_play), Toast.LENGTH_SHORT).show(); break; case RANDOM_PLAY: imageView1_play_mode.setImageResource(R.mipmap.app_mode_single); //imageView1_play_mode.setTag(SINGLE_PLAY); playService.setPlay_mode(SINGLE_PLAY); //Toast.makeText(getApplicationContext(),"單曲循環",Toast.LENGTH_SHORT).show();//這句也可以 //Toast.makeText(PlayActivity.this, "單曲循環", Toast.LENGTH_SHORT).show();//這句也可以 Toast.makeText(PlayActivity.this, getString(R.string.single_play), Toast.LENGTH_SHORT).show(); break; case SINGLE_PLAY: imageView1_play_mode.setImageResource(R.mipmap.app_mode_order); //imageView1_play_mode.setTag(ORDER_PLAY); playService.setPlay_mode(ORDER_PLAY); //Toast.makeText(getApplicationContext(),"順序播放",Toast.LENGTH_SHORT).show();//這句也可以 //Toast.makeText(PlayActivity.this, "順序播放", Toast.LENGTH_SHORT).show();//這句也可以 Toast.makeText(PlayActivity.this, getString(R.string.order_play), Toast.LENGTH_SHORT).show(); break; } break; } case R.id.imageView1_favorite: {//收藏按鈕 //在vo.Mp3Info裡 private long mp3InfoId;//在收藏音樂時用於保存原始ID Mp3Info mp3Info = playService.mp3Infos.get(playService.getCurrentPosition());//查出歌曲 System.out.println(mp3Info); try { Mp3Info loveMp3Info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId","=",getId(mp3Info)));//查出歌曲,SQL語句 System.out.println(loveMp3Info); if (loveMp3Info==null){//返回值為null,則一定需要save System.out.println("不在音樂收藏數據庫中 保存音樂數據 原始數據: " + mp3Info); mp3Info.setMp3InfoId(mp3Info.getId()); mp3Info.setIsLove(1); System.out.println(mp3Info); app.dbUtils.save(mp3Info);//在音樂收藏數據庫 保存音樂 System.out.println("save"); imageView1_favorite.setImageResource(R.mipmap.app_love_selected); //以下是:調試使用,保存以後再查一遍 loveMp3Info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId","=",getId(mp3Info)));//查出歌曲,SQL語句 System.out.println("調試使用,保存以後再查一遍 最新數據: " + loveMp3Info); }else {//返回值不為null,則一定需要update System.out.println("在音樂收藏數據庫中 更新音樂數據 原始數據: " + loveMp3Info); int isLove = loveMp3Info.getIsLove(); if (isLove==1){//返回值不為null,且,isLove為1時;設置isLove為0,同時顯示為'未收藏' loveMp3Info.setIsLove(0); imageView1_favorite.setImageResource(R.mipmap.app_love_unselected); }else {//返回值不為null,且,isLove為0時;設置isLove為1,同時顯示為'已收藏' loveMp3Info.setIsLove(1); imageView1_favorite.setImageResource(R.mipmap.app_love_selected); } System.out.println("update"); app.dbUtils.update(loveMp3Info,"isLove");//更新loveMp3Info數據 //以下是:調試使用,更新以後再查一遍 loveMp3Info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId", "=", getId(mp3Info)));//查出歌曲,SQL語句 System.out.println("調試使用,更新以後再查一遍 最新數據: " + loveMp3Info); } } catch (DbException e) { e.printStackTrace(); } break; } default: break; } } }
package com.iwanghang.drmplayer; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.TransitionDrawable; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import com.astuetz.PagerSlidingTabStrip; import com.astuetz.viewpager.extensions.sample.QuickContactFragment; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import java.nio.charset.CoderMalfunctionError; import java.util.ArrayList; public class MainActivity extends BaseActivity { // private final Handler handler = new Handler(); private PagerSlidingTabStrip tabs; private ViewPager pager; private MyPagerAdapter adapter; public DRMPlayerApp app;//取出全局對象 方便調用 // private Drawable oldBackground = null; // private int currentColor = 0xFFC74B46; private MyMusicListFragment myMusicListFragment; private NetMusicListFragment netMusicListFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //取出全局對象 方便調用 app = (DRMPlayerApp) getApplication(); setContentView(R.layout.activity_main); tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); pager = (ViewPager) findViewById(R.id.pager); adapter = new MyPagerAdapter(getSupportFragmentManager()); pager.setAdapter(adapter); final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics()); pager.setPageMargin(pageMargin); tabs.setViewPager(pager); //修改主界面顏色,稍後修復功能,暫時使用默認顏色 //changeColor(currentColor); //綁定服務 //服務在加載SplashActivity(歡迎頁面)的時候,已經啟動 //bindPlayService(); //這裡,我在MyMusicListFragment裡面綁定,而沒有在MainActivity裡綁定 } @Override public void publish(int progress) { //更新進度條 } @Override public void change(int position) { //更新歌曲位置.按鈕的狀態等信息 /** * 本地音樂的播放UI實際上在MyMusicListFragment中,所以要 * 先在MyMusicListFragmen中,寫入public void changeUIStatus(int position){} * 然後,傳參過去 */ if (pager.getCurrentItem()==0){//如果頁面等於0,則說明選中的是第一個頁面,我的音樂頁面 myMusicListFragment.loadData();//初始化數據 myMusicListFragment.changeUIStatusOnPlay(position); }else if (pager.getCurrentItem()==1){ } } // @Override // public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.main, menu); // return true; // } // // @Override // public boolean onOptionsItemSelected(MenuItem item) { // // switch (item.getItemId()) { // // case R.id.action_contact: // QuickContactFragment dialog = new QuickContactFragment(); // dialog.show(getSupportFragmentManager(), "QuickContactFragment"); // return true; // // } // // return super.onOptionsItemSelected(item); // } // private void changeColor(int newColor) { // // tabs.setIndicatorColor(newColor); // // // change ActionBar color just if an ActionBar is available // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // // Drawable colorDrawable = new ColorDrawable(newColor); // Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom); // LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable }); // // if (oldBackground == null) { // // if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // ld.setCallback(drawableCallback); // } else { // getActionBar().setBackgroundDrawable(ld); // } // // } else { // // TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld }); // // // workaround for broken ActionBarContainer drawable handling on // // pre-API 17 builds // // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4 // if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // td.setCallback(drawableCallback); // } else { // getActionBar().setBackgroundDrawable(td); // } // // td.startTransition(200); // // } // // oldBackground = ld; // // // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler // getActionBar().setDisplayShowTitleEnabled(false); // getActionBar().setDisplayShowTitleEnabled(true); // // } // // currentColor = newColor; // // } // public void onColorClicked(View v) { // // int color = Color.parseColor(v.getTag().toString()); // changeColor(color); // // } // @Override // protected void onSaveInstanceState(Bundle outState) { // super.onSaveInstanceState(outState); // outState.putInt("currentColor", currentColor); // } // @Override // protected void onRestoreInstanceState(Bundle savedInstanceState) { // super.onRestoreInstanceState(savedInstanceState); // currentColor = savedInstanceState.getInt("currentColor"); // changeColor(currentColor); // } // private Drawable.Callback drawableCallback = new Drawable.Callback() { // @Override // public void invalidateDrawable(Drawable who) { // getActionBar().setBackgroundDrawable(who); // } // // @Override // public void scheduleDrawable(Drawable who, Runnable what, long when) { // handler.postAtTime(what, when); // } // // @Override // public void unscheduleDrawable(Drawable who, Runnable what) { // handler.removeCallbacks(what); // } // }; public class MyPagerAdapter extends FragmentPagerAdapter { private final String[] TITLES = { "本地音樂", "網絡音樂"}; public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } @Override public int getCount() { return TITLES.length; } @Override public Fragment getItem(int position) { //return SuperAwesomeCardFragment.newInstance(position); if(position==0){ if(myMusicListFragment==null){ myMusicListFragment = MyMusicListFragment.newInstance(); } return myMusicListFragment; }else if (position == 1){ if(netMusicListFragment==null){ netMusicListFragment = NetMusicListFragment.newInstance(); } return netMusicListFragment; } return null; //return MyMusicListFragment.newInstance(); } } @Override public boolean onCreateOptionsMenu(Menu menu) {//填充菜單 getMenuInflater().inflate(R.menu.main_menu,menu); return true; } public boolean onOptionsItemSelected(MenuItem item){//菜單中事件 switch (item.getItemId()){ case R.id.ilove: Intent intent = new Intent(this,MyLoveMusicActivity.class); startActivity(intent); System.out.println("我的收藏"); break; case R.id.near_play: Intent intent2 = new Intent(this,MyRecordMusicActivity.class); startActivity(intent2); System.out.println("最近播放"); break; } return true; } @Override protected void onDestroy() { super.onDestroy(); //保存當前播放的一些狀態值 DRMPlayerApp app = (DRMPlayerApp) getApplication(); SharedPreferences.Editor editor = app.sp.edit(); //保存 當前正在播放的歌曲的位置 editor.putInt("currentPosition",playService.getCurrentPosition()); //保存 播放模式 editor.putInt("play_mode",playService.getPlay_mode()); //保存 提交 editor.commit(); //創建DRMPlayerApp繼承Application,同時需要在把AndroidManiFest中的public換成DRMPlayerApp //在DRMPlayerApp的onCreate中 實例化 SharedPreferences //在MainActivity的onDestroy中 保存狀態值 //在PlayService的onCreate中 恢復狀態值 } }解決ActionBar左上角圖片不顯示問題:
Android-通過Java代碼來實現屬性動畫除了可以使用定義xml文件來設置動畫之外,還可以使用java代碼來進行控制動畫。示例如下:布局文件: 主活動:
1. Dom概述Dom方式創建XML,應用了標准xml構造器 javax.xml.parsers.DocumentBuilder 來創建 XML 文檔,需要導入以下內容j
Android 軟鍵盤自動彈出與關閉在我們寫修改信息或者搜索,修改密碼等界面的時候,用戶進入這個界面的主要目的就是輸入修改/查找 某些信息,為了用戶體驗應該自動彈出軟鍵盤
前言:關於使用ViewPageIndicator和ViewPager實現Tab導航,在開發社區裡已經有一堆的博客對其進行了介紹,如果我還在這裡寫如何去實現,那簡直就是老生