自定义View继承现有的Toast,实现订单提醒的Toast,从左下角显示然后退出
/**
* Created by on 16-2-4.
*/
public class NotifyToast extends Toast {
private long lastShowTime;
public static NotifyToast instance;
public static synchronized NotifyToast getInstance(Context context){
if(instance==null){
instance = new NotifyToast(context,"你有未处理的订单,请抓紧处理",3000);
}
return instance;
}
private MediaPlayer mPlayer;
/**
* Construct an empty Toast object. You must call {@link #setView} before you
* can call {@link #show}.
*
* @param context The context to use. Usually your {@link Application}
* or {@link Activity} object.
*/
public NotifyToast(Context context,CharSequence text, int duration) {
super(context);
mPlayer = MediaPlayer.create(context, R.raw.neworder);
//2016年08月02日10:51:32 不需要设置完成后释放资源的回调,整段注释掉了
// mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
// {
// @Override
// public void onCompletion(MediaPlayer mp)
// {
mp.release();// 释放资源。让资源得到释放;;
// }
// });
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
View v = inflate.inflate(R.layout.new_data_toast, null);
// v.setMinimumWidth(dm.widthPixels);// 设置控件最小宽度为手机屏幕宽度
setView(v);
this.setDuration(duration);// 设置 显示多长时间;;;;
this.setGravity(Gravity.LEFT|Gravity.BOTTOM, (int) (dm.density * 10), (int) (dm.density * 10));
}
public void show() {
long nowTime = new Date().getTime();
if (nowTime - lastShowTime > 10000) {
super.show();
if(mPlayer!=null) {
mPlayer.start();
}
lastShowTime = nowTime;
}
}
}