标签:
实现功能:通知媒体库更新
package com.iwanghang.drmplayer; import android.app.AlertDialog; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.graphics.Path; import android.media.MediaScannerConnection; 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.util.Log; import android.view.View; import android.widget.Toast; import com.iwanghang.drmplayer.utils.Constant; import com.iwanghang.drmplayer.utils.DownloadUtils; import com.iwanghang.drmplayer.utils.MediaScanner; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.utils.SearchMusicUtils; import com.iwanghang.drmplayer.vo.SearchResult; import java.io.File; import java.io.IOException; import java.util.ArrayList; /** * Created by iwanghang on 16/5/3. * DownloadDialogFargment */ public class DownloadDialogFragment extends DialogFragment { 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)}; } //创建对话框的事件方法 @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; } } }); return builder.show(); } //下载音乐 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); //更新 本地音乐列表 } @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); } }
Android开发本地及网络Mp3音乐播放器(十九)通知媒体库更新刚下载的MP3
标签:
原文地址:http://blog.csdn.net/iwanghang/article/details/51427158