安卓点歌系统

public class Favor_Song {

    private static Favor_Song mfavor_song = null; 
    private Context mcontext= null;
    private List mlist= new ArrayList();
    private String sfile= null;
    private final int ETF_Favor_MAX = 50;
    private final int ETF_SAVE_Favor = 256;
    private Handler mhandler;

    private Favor_Song()
    {
        mhandler = new Handler(){
            public void handleMessage(Message message)
            {
                switch(message.what){
                case ETF_SAVE_Favor:
                    savefile();
                    break;
                }
            }
        };
    }

    public static Favor_Song getInstance()
    {
        if(mfavor_song == null)
            mfavor_song = new Favor_Song();
        return mfavor_song;
    }

    private void loadfile()
    {
        try{
            JSONArray jsonarray = new JSONArray(etfStatic.load_file(sfile));
            int i = 0;
            while(i < jsonarray.length()){
                Song b1 = Song.jsontosong(jsonarray.getJSONObject(i));
                if(b1 != null)
                    add(b1);
                i++;
            }    
        }catch(Exception ex){}
    }

    private Song find(Song b1)
    {
        String s;
        String s1;
        int i;
        s = b1.getmusic_id();
        s1 = b1.getmusic_name();
        i = 0;
        Song b2 = null;
        while(i < mlist.size()){
            String s2;
            String s3;
            b2 = (Song)mlist.get(i);
            s2 = b2.getmusic_id();
            s3 = b2.getmusic_name();
            if(s2.equals(s) && s3.equals(s1)) break;
            i++;
        }
        return b2;
    }

    private void savefile()
    {
        try
        {
            JSONArray jsonarray = new JSONArray();
            Iterator iterator = mlist.iterator();
            while(iterator.hasNext())
            {
                jsonarray.put(((Song)iterator.next()).tojson());
            };
            etfStatic.save_file(sfile, jsonarray.toString());
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
    }

    public void init(Context context)
    {
        mcontext = context;
        sfile = String.valueOf(mcontext.getCacheDir().getAbsolutePath())+"/music_favorite";
        loadfile();
    }

    public void add(Song b1)
    {
        if(find(b1) == null){
            mlist.add(0, b1);
            if(mlist.size() > ETF_Favor_MAX)
                mlist.remove(ETF_Favor_MAX);
            if(mhandler != null)
            {
                mhandler.removeMessages(ETF_SAVE_Favor);
                mhandler.sendEmptyMessageDelayed(ETF_SAVE_Favor, 5000L);
            }
        } 
    }

    public List getlist()
    {
        return mlist;
    }

    public void remove(Song b1)
    {
        Song b2 = find(b1);
        if(b2 != null)
        {
            mlist.remove(b2);
            if(mhandler != null)
            {
                mhandler.removeMessages(ETF_SAVE_Favor);
                mhandler.sendEmptyMessageDelayed(ETF_SAVE_Favor, 5000L);
            }
        }
    }

    public boolean check(Song b1)
    {
        boolean flag;
        if(find(b1) != null)
            flag = true;
        else
            flag = false;
        return flag;
    }
}

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值