@Override public void onReceive(Context arg0, Intent intent) { if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(intent.getAction())) { // 获取媒体库信息 ArrayList<Music> musics = Musicdata.getMultiDatas(context); // 更新listview MyApplication.getInstance().setMusics(musics); localLayout.Refresh(LocalLayout.CHANGE_LIST, musics); progressDialog.cancel(); context.sendBroadcast(new Intent(Constant.ACTION_LISTCHANGED)); } else if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(intent.getAction())) { // 显示对话框 progressDialog = new ProgressDialog(context); progressDialog.setMessage("列表正在刷新中,请等待......"); progressDialog.show(); } }
@Override public boolean onContextItemSelected(MenuItem item) { if (item.getGroupId() == 1) { // 远程音乐 AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); final Music music = (Music) netLayout.netlistview.getAdapter().getItem(menuInfo.position); switch (item.getItemId()) { case MENU_DOWN_MUSIC: netLayout.Refresh(NetLayout.DOWN_MUSIC, menuInfo.position); if (!music.isLoaded()) { DownMusic(music); } else { AlertDialog.Builder builder = new Builder(context); builder .setTitle("注意") .setMessage("音乐已经存在确认要重复下载吗?") .setPositiveButton( "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DownMusic(music); } }) .setNegativeButton("取消", null) .create() .show(); } break; case MENU_LISTEN_MUSIC: progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("音乐正在缓冲中,请等待......"); progressDialog.show(); Intent intent = new Intent(Constant.ACTION_NET_PLAY); intent.putExtra("net_music", music); sendBroadcast(intent); netLayout.Refresh(NetLayout.LISTEN_MUSIC, menuInfo.position); break; } } else if (item.getGroupId() == 0) { // 获得menuinfo对象 ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); // 获得一个packedPosition long packedPosition = info.packedPosition; // 根据packedPosition,获取Expandablelistview中被选中项的groupPosition // 组id int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition); // childid int childPosition = ExpandableListView.getPackedPositionChild(packedPosition); // 获取组对象 MusicGroup group = (MusicGroup) favoriteLayout.exgroupview.getExpandableListAdapter().getGroup(groupPosition); MusicItemDao musicDao = new MusicItemDao(this); switch (item.getItemId()) { case MENU_ADD_GROUP: // 添加分组 favoriteLayout.Refresh(FavoriteLayout.ADD_GROUP); break; case MENU_CHI_PLAY_ONE: // 播放当前选择歌曲 if (childPosition != -1) { MusicItem musicItem = (MusicItem) favoriteLayout .exgroupview .getExpandableListAdapter() .getChild(groupPosition, childPosition); Intent intent = new Intent(Constant.ACTION_FIND); musicname = Musicdata.getMusicbyid(context, String.valueOf(musicItem.getMusicId())) .getMusicName(); intent.putExtra("name", musicname); sendBroadcast(intent); } break; case MENU_CHI_PLAYALL: // 播放当前分组 MyApplication.getInstance() .setMusics( Musicdata.getmusicsByitem(musicDao.getMusicsByGroup(group.getId()), context)); sendBroadcast(new Intent(Constant.ACTION_LISTCHANGED)); break; case MENU_CHI_REMOVE: // 从分组中移除 if (childPosition != -1) { // 获取musicitem对象 MusicItem musicItems = (MusicItem) favoriteLayout .exgroupview .getExpandableListAdapter() .getChild(groupPosition, childPosition); // 获取该musicitem的id,删除 musicDao.deleteItemById(musicItems.getId()); // 更新界面 favoriteLayout.Refresh(FavoriteLayout.REFRESH_GROUP); } break; case MENU_CLEAR_GROUP: // 清空分组 musicDao.deleteItemByGroupid(group.getId()); favoriteLayout.Refresh(FavoriteLayout.REFRESH_GROUP); break; case MENU_DELETE_GROUP: // 删除分组 groupDao.deleteGroup(group.getId()); favoriteLayout.Refresh(FavoriteLayout.REFRESH_GROUP); break; case MENU_UPDATE_GROUP: // 修改分组名称 updatagroup(group.getId()); break; case MENU_SETRING: // 铃声设置 MusicItem musicItem1 = (MusicItem) favoriteLayout .exgroupview .getExpandableListAdapter() .getChild(groupPosition, childPosition); musicname = Musicdata.getMusicbyid(this.context, String.valueOf(musicItem1.getMusicId())) .getMusicName(); setMoreRing(); break; } } return super.onContextItemSelected(item); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); MyApplication.getInstance().addActivity(this); context = this; inflater = LayoutInflater.from(this); // 获取上次退出时的播放位置 initView(); startService(new Intent(context, MusicPlayerService.class)); // 启动service Intent intent = new Intent(this, DownloadService.class); startService(intent); this.getApplicationContext().bindService(intent, conn, BIND_AUTO_CREATE); // 创建音乐目录 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File root = Environment.getExternalStorageDirectory(); dir = new File(root, "TMusic"); if (!dir.exists()) { dir.mkdir(); } } myReciver = new MusicReciver(); reciver = new refreshReciver(); groupDao = new MusicGroupDao(context); }