編輯:關於Android編程
除了下面說明的一個問題,還有一些BUG有待修復,後續博文將會繼續更新
//DownloadDialogFragment回傳的是否下載成功的回傳值}
實現效果如圖:
package com.iwanghang.drmplayer; import android.app.AlertDialog; import android.app.Dialog; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast; import com.iwanghang.drmplayer.adapter.MyMusicListAdapter; import com.iwanghang.drmplayer.utils.Constant; import com.iwanghang.drmplayer.utils.DownloadUtils; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import com.iwanghang.drmplayer.vo.SearchResult; import java.io.File; import java.util.ArrayList; /** * Created by iwanghang on 16/5/3. * DownloadDialogFargment */ public class DownloadDialogFragment extends DialogFragment { private MyMusicListFragment myMusicListFragment; private ArrayListmp3Infos; private MyMusicListAdapter myMusicListAdapter; private SearchResult searchResult;//當前要下載的歌曲對象 private MainActivity mainActivity; public static DownloadDialogFragment newInstance(SearchResult searchResult){ DownloadDialogFragment downloadDialogFragment = new DownloadDialogFragment(); downloadDialogFragment.searchResult = searchResult; return downloadDialogFragment; } private String[] items; @Override public void onAttach(Context context) { super.onAttach(context); mainActivity = (MainActivity) getActivity(); //items = new String[]{"下載","取消"}; items = new String[]{getString(R.string.download),getString(R.string.cancel)}; } // 回調接口 //public interface DownloadSelectListener { // 是否選擇(點擊)下載.監聽.按鈕回調接口 // void downloadSelectListener(int isDownload); // 回傳一個int,是否選擇(點擊)下載按鈕 ; 回傳0,表示下載;回傳1,表示取消 //} //創建對話框的事件方法 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity); builder.setCancelable(true);//返回鍵可以取消 builder.setItems(items,new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: //執行下載 downloadMusic(); break; case 1://取消 dialog.dismiss(); break; } //DownloadSelectListener listener = (DownloadSelectListener) getActivity(); // 回調接口 //listener.downloadSelectListener(which);//回傳0,表示下載;回傳1,表示取消 } }); return builder.show(); } // 回調接口 public interface DownloadSuccessListener { // 下載是否成功.監聽.按鈕回調接口 void downloadSuccessListener(String isDownloadSuccess); // 回傳一個字符串 } //下載音樂 private void downloadMusic() { Toast.makeText(mainActivity, "正在下載:" + searchResult.getMusicName(), Toast.LENGTH_LONG).show(); DownloadUtils.getsInstance().setListener(new DownloadUtils.OnDownloadListener() { @Override public void onDowload(String mp3Url) { //下載成功 Toast.makeText(mainActivity, "下載成功", Toast.LENGTH_SHORT).show(); System.out.println("DownloadDialogFragment.downloadMusic @ = " + Environment.getExternalStorageDirectory()+Constant.DIR_MUSIC + "/" + mp3Url); //更新媒體庫 scanFile(mainActivity,Environment.getExternalStorageDirectory()+Constant.DIR_MUSIC + "/" + mp3Url); //更新 本地音樂列表 , 這個功能放在MainActivity實現 //DownloadSuccessListener listener = (DownloadSuccessListener) getActivity(); // 空指針異常,因為Fragment已經銷毀,所以getActivity()==null,需要使用下面的寫法 DownloadSuccessListener listener = mainActivity; // 回調接口 System.out.println("DownloadDialogFragment.downloadMusic.listener = " + listener); listener.downloadSuccessListener(mp3Url); // 回傳一個字符串 ,回傳什麼都行 ,只是告訴MainActivity ,已經下載成功了新的歌曲 } @Override public void onFailed(String error) { //下載失敗 Toast.makeText(mainActivity, error, Toast.LENGTH_SHORT).show(); } }).download(searchResult); } /** * 通知媒體庫更新文件 * @param context * @param filePath 文件全路徑 * * */ public void scanFile(Context context, String filePath) { Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); scanIntent.setData(Uri.fromFile(new File(filePath))); context.sendBroadcast(scanIntent); } /** * 更新本地音樂列表 */ //private void loadDataAndChangeUI() { // if(myMusicListFragment==null){ // myMusicListFragment = MyMusicListFragment.newInstance(); // } // //mp3Infos = MediaUtils.getMp3Infos(mainActivity); // //myMusicListAdapter = new MyMusicListAdapter(mainActivity,mp3Infos); // //listView_my_music.setAdapter(myMusicListAdapter); // myMusicListFragment.loadData();//初始化數據 // myMusicListFragment.changeUIStatusOnPlay(0); //} }
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 android.widget.Toast; 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 implements DownloadDialogFragment.DownloadSuccessListener { // 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); /** android TypedValue.applyDimension()的作用 這個方法是轉變為標准尺寸的一個函數,例如 int size =(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20,context.getResources().getDisplayMetrics()); 這裡COMPLEX_UNIT_SP是單位,20是數值,也就是20sp。 */ 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,則說明選中的是第一個頁面,我的音樂頁面 System.out.println("MainActivity.change.pager.getCurrentItem()==0"); myMusicListFragment.loadData();//初始化數據 myMusicListFragment.changeUIStatusOnPlay(position); }else if (pager.getCurrentItem()==1){ System.out.println("MainActivity.change.pager.getCurrentItem()==1"); } } //DownloadDialogFragment回傳的是否點擊下載按鈕的回傳值 //@Override //public void downloadSelectListener(int isDownload) { // if (isDownload == 0){ // Toast.makeText(MainActivity.this, "DownloadDialogFragment.下載點擊事件.回傳給.MainActivity", Toast.LENGTH_SHORT).show(); // // }else if (isDownload == 1){ // Toast.makeText(MainActivity.this, "DownloadDialogFragment.取消點擊事件.回傳給.MainActivity", Toast.LENGTH_SHORT).show(); // //MyMusicListFragment.newInstance(); // } //} //DownloadDialogFragment回傳的是否下載成功的回傳值 @Override public void downloadSuccessListener(String isDownloadSuccess) { System.out.println("MainActivity.downloadSuccessListener = " + isDownloadSuccess); if (isDownloadSuccess.length() > 0){ //Toast.makeText(MainActivity.this, "DownloadDialogFragment.下載成功.回傳給.MainActivity = " + isDownloadSuccess, Toast.LENGTH_SHORT).show(); //問題:下載完成後,確實通知媒體庫更新了,但是馬上初始化數據,可能媒體庫還沒有更新 //臨時解決:在ActionBar上,添加一個刷新按鈕 //永久解決:通過mp3Infos.size()的改變,來判斷媒體庫已經更新 //更新本地音樂列表 myMusicListFragment.loadData();//初始化數據 myMusicListFragment.newInstance();//重新實例化一下本地音樂Fragment,加載新的數據 } } // @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){ System.out.println("MainActivity.MyPagerAdapter.position==0"); if(myMusicListFragment==null){ myMusicListFragment = MyMusicListFragment.newInstance(); } return myMusicListFragment; }else if (position == 1){ System.out.println("MainActivity.MyPagerAdapter.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.newMyMusic: myMusicListFragment.loadData();//初始化數據 myMusicListFragment.newInstance();//重新實例化一下本地音樂Fragment,加載新的數據 System.out.println("刷新"); break; 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中 恢復狀態值 } }
Android中的Toast是很常見的一個消息提示框,但是默認的消息提示框就是一行純文本,所以我們可以為它設置一些其他的諸如是帶上圖片的消息提示。 實現這個很簡單: 就是
第一篇博客裡面有介紹一篇關於自動換行實現諸多自定義控件跟各種效果的博文,但是礙於當初技術能力有限,寫的jar包裡的代碼亂七八糟,在最近忙完了手頭的工作,不經意間翻看了之前
前面兩篇博客,詳細分析了Native與Javascript通信的過程,可以滿足絕大部分場景下Native和Javascript的相互調用,但是仍然有不健全的情況。比如Ja
小米手環2支持哪些手機?小米手環2准備開放購買了,相比於小米手環一代,新款的小米手環2更值得大家購買,那麼在購買之前,先要了解小米手環2是否支持自己的手機,