您的位置:首页 > 其它

解决Toast在部分机型上关闭通知时无法弹出

2018-03-22 09:49 405 查看
本文章转载至:http://blog.csdn.net/u010998832/article/details/72885514
根据原文作者的内容结合于我的项目进行了一些改动。

首先TiastUtils工具类:import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Stack;

public class ToastUtils {

public static final int LENGTH_SHORT = 0x00;
public static final int LENGTH_LONG = 0x01;

private final int ANIMATION_DURATION = 600;

public Context mContext;
public String msg;
private int HIDE_DELAY = 2000;

// public static boolean isRunning = false;

private Handler mHandler = new Handler();

public static Stack<ToastUtils> stack = new Stack();

// 表示吐司里显示的文字
public static ToastUtils makeText(Context context, String message,
int HIDE_DELAY) {
ToastUtils utils = new ToastUtils();
utils.mContext = context;
utils.msg = message;

if (HIDE_DELAY == LENGTH_LONG) {
utils.HIDE_DELAY = 2500;
} else {
utils.HIDE_DELAY = 1500;
}

return utils;
}

public static void wakeUp() {
// isRunning = true;
if (!stack.empty()) {
ToastUtils util = stack.pop();
util.doshow();

} else {
// isRunning = false;
}

}

public void doshow() {
final ViewGroup container = (ViewGroup) ((Activity) mContext)
.findViewById(android.R.id.content);
final View mView = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.toast_layout, null);
container.addView(mView);

final LinearLayout mContainer = (LinearLayout) mView.findViewById(R.id.mbContainer);
mContainer.setVisibility(View.GONE);
TextView mTextView = (TextView) mView.findViewById(R.id.mbMessage);
mTextView.setText(msg);

// 显示动画
AlphaAnimation mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
// 消失动画
final AlphaAnimation mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mFadeOutAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation
.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// 消失动画后更改状态为 未显示

}

@Override
public void onAnimationEnd(Animation animation) {
// 隐藏布局,不使用remove方法为防止多次创建多个布局
mContainer.setVisibility(View.GONE);
container.removeView(mView);
wakeUp();
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});
mContainer.setVisibility(View.VISIBLE);

mFadeInAnimation.setDuration(ANIMATION_DURATION);

mContainer.startAnimation(mFadeInAnimation);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mContainer.startAnimation(mFadeOutAnimation);
}
}, HIDE_DELAY);
}

public void show() {
stack.push(this);
// if (!isRunning) {
wakeUp();

// }
}

}
在这其中我将isRunning给注释了,解释:这个字符串就是防止重复点击重复出现的,但是在我测试的时候有时候会发生之触发一次,就不会再次出现了,为防止意外,就直接去掉这个限制。
继续布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mbContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:gravity="bottom|center"
android:orientation="vertical"
android:paddingLeft="50dp"
android:paddingRight="50dp">

<LinearLayout android:id="@+id/toast_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="50dp"
android:padding="8dp">

<TextView android:id="@+id/mbMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:gravity="center"
android:alpha="0.8"
android:background="@drawable/shape_toastutils_bg"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:textSize="12sp"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>这个布局就是绘制一个吐司的背景以及要显示文字的view
吐司背景的样式:<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<corners android:radius="40dp" />
</shape>为了更贴近于原生的吐司。
使用方法:
ToastUtils.makeText(context, "", ToastUtils.LENGTH_LONG).show();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐