码迷,mamicode.com
首页 > 移动开发 > 详细

【Android N 7.1.1】 锁屏之上显示Toast

时间:2017-06-17 15:59:44      阅读:650      评论:0      收藏:0      [点我收藏+]

标签:man   imp   make   package   sys   off   void   tin   users   

package com.android.systemuirom.keyguard;

import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

public class KeyguardToast {

    private static KeyguardToast sInstance;
    private Toast mToast = null;

    public static KeyguardToast getInstance() {
        if (sInstance == null) {
            sInstance = new KeyguardToast();
        }
        return sInstance;
    }

    private KeyguardToast() {
        // ignore
    }

    public Toast makeText(Context context, CharSequence text, int duration,
            boolean needOffsetPosition) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        mToast = Toast.makeText(context, text, duration);

        mToast.getWindowParams().type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
        mToast.getWindowParams().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
        mToast.getWindowParams().flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

        // Toast偏移位置
        if (needOffsetPosition) {
            // set offset position
            mToast.setGravity(Gravity.CENTER, 0, -300);
        }
        return mToast;
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }
}

 

【Android N 7.1.1】 锁屏之上显示Toast

标签:man   imp   make   package   sys   off   void   tin   users   

原文地址:http://www.cnblogs.com/onelikeone/p/7040243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!