Android Toast 保持一直显示。

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiafangxingg/article/details/79241300

package android.widget;

import android.app.ITransientNotification;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import java.lang.reflect.Field;

public class ToastUtils {

    private static Toast mToast = null ;
    public static final int MEDIA_TYPE_MUSIC = 0x00000001;
    public static final int MEDIA_STATUS_LOADING = 0x10000000;

    public static void showMediaStatusToast(Context context, int mediaType, String msg) {
        TextView toastMsg = null;
        ImageView toastImage = null;
        if (mToast == null) {
            LayoutInflater inflate = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View toastView = inflate.inflate(com.android.internal.R.layout.media_ready_toast, null);
            toastMsg = ((TextView) toastView.findViewById(com.android.internal.R.id.tv_toast_clear));
            toastImage = (ImageView) toastView.findViewById(com.android.internal.R.id.iv_toast_clear);

            mToast = new Toast(context.getApplicationContext());
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setView(toastView);
        } else {
            toastMsg = (TextView) mToast.getView().findViewById(com.android.internal.R.id.tv_toast_clear);
            toastImage = (ImageView) mToast.getView().findViewById(com.android.internal.R.id.iv_toast_clear);
        }

        switch(mediaType) {
            case MEDIA_TYPE_MUSIC://music
                toastImage.setImageResource(com.android.internal.R.drawable.img_no_usb);
                break;
            case MEDIA_STATUS_LOADING://loading
                toastImage.setImageResource(com.android.internal.R.drawable.media_progress);
                toastMsg.setText(msg);
                showProgress();
                break;
        }
        if (mediaType != MEDIA_STATUS_LOADING) {
            toastMsg.setText(msg);
            mToast.show();
        }

    }

    private static void showProgress() {
        if (mToast != null) {
            try {
                Field toastNextView = mToast.getClass().getDeclaredField("mNextView");
                toastNextView.setAccessible(true);
                Object toastView = toastNextView.get(mToast);
                toastNextView.setAccessible(false);
                Field mTN = mToast.getClass().getDeclaredField("mTN");
                mTN.setAccessible(true);
                Object tn = mTN.get(mToast);
                mTN.setAccessible(false);
                Field tnView = tn.getClass().getDeclaredField("mNextView");
                tnView.setAccessible(true);
                tnView.set(tn, toastView);
                tnView.setAccessible(false);
                ITransientNotification notification = (ITransientNotification) mTN.get(mToast);
                notification.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void hideProgress() {
        if (mToast != null) {
            try {
                Field mTN = mToast.getClass().getDeclaredField("mTN");
                ITransientNotification notification = (ITransientNotification) mTN.get(mToast);
                notification.hide();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页