Android一些常用类的封装

PlaySound

效果图:


image.png

PlaySoundActivity.java:

package com.example.playsoundtest01.media;

import android.app.Activity;
import android.media.MediaPlayer;

public class PlaySoundActivity {
    MediaPlayer mp;

    /**
     * 开始播放
     *
     * @param activity
     * @param isLoop
     * @param sound
     */
    public void playSound(Activity activity, final boolean isLoop, int sound) {
        mp = MediaPlayer.create(activity, sound);
        new Thread() {
            @Override
            public void run() {
                super.run();
                if (isLoop == true) {
                    mp.setLooping(true);
                }
                mp.start();
            }
        }.start();
    }

    /**
     * 停止播放
     */
    public void stopSound() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                mp.stop();
            }
        }.start();
    }
}

GetTime

public void GetTime() {
        cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        year = String.valueOf(cal.get(Calendar.YEAR));
        month = String.valueOf(cal.get(Calendar.MONTH) + 1);
        day = String.valueOf(cal.get(Calendar.DATE));
        if (cal.get(Calendar.AM_PM) == 0)
            hour = String.valueOf(cal.get(Calendar.HOUR));
        else
            hour = String.valueOf(cal.get(Calendar.HOUR) + 12);
        minute = String.valueOf(cal.get(Calendar.MINUTE));
        time = "时间:" + year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分";
    }

你可能感兴趣的:(Android一些常用类的封装)