Android keyguard之上如何显示Toast

锁屏之上应该如何显示Toast呢? 看下面的实现:

public class KeyguardToast {

    public static Toast makeText(Context context, CharSequence text, int duration) {
        Toast toast = Toast.makeText(context, text, duration);
        toast.getWindowParams().type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
        toast.getWindowParams().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
        toast.getWindowParams().flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

        // set offset position
        toast.setGravity(Gravity.CENTER, 0, 400);
        return toast;
    }
}

锁屏之上如何显示Dialog?看下面的实现:

public class SystemUIDialog extends AlertDialog {

    private final Context mContext;

    public SystemUIDialog(Context context) {
        super(context, R.style.Theme_SystemUI_Dialog);
        mContext = context;

        getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.setTitle(getClass().getSimpleName());
        getWindow().setAttributes(attrs);
    }

    public void setShowForAllUsers(boolean show) {
        if (show) {
            getWindow().getAttributes().privateFlags |=
                    WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
        } else {
            getWindow().getAttributes().privateFlags &=
                    ~WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
        }
    }

    public void setMessage(int resId) {
        setMessage(mContext.getString(resId));
    }

    public void setPositiveButton(int resId, OnClickListener onClick) {
        setButton(BUTTON_POSITIVE, mContext.getString(resId), onClick);
    }

    public void setNegativeButton(int resId, OnClickListener onClick) {
        setButton(BUTTON_NEGATIVE, mContext.getString(resId), onClick);
    }
}

设置Toast显示字体大小颜色

LayoutInflater inflater = LayoutInflater
            .from(getApplicationContext());
    View view = inflater.inflate(R.layout.my_toast,
            (ViewGroup) findViewById(R.id.toast_layout_root));
    TextView textView = (TextView) view.findViewById(R.id.text);

    SpannableString ss = new SpannableString("扫描成功");

    ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 4,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(ss);

    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(view);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();

mytoast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >

    <TextView
        android:layout_marginTop="800dip"
        android:textSize="25dip"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="50dip"

        android:textColor="@color/black" />

</LinearLayout>

Toast:

Toast toast = Toast.makeText(mContext, R.string.unlock_message, Toast.LENGTH_SHORT);
toast.getWindowParams().type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
toast.getWindowParams().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
toast.getWindowParams().flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

// set offset position
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();

Dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
 mLockDialog = builder.create();
 mLockDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL);
 mLockDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
         | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
 mLockDialog.getWindow().setGravity(Gravity.BOTTOM);
 mLockDialog.setTitle(mContext.getResources().getString(R.string.dialog_alert_title));
 mLockDialog.setMessage(mContext.getResources().getString(R.string.unlock_message));
 mLockDialog.show();

results matching ""

    No results matching ""