当前位置: 移动技术网 > IT编程>移动开发>Android > Android 5.0以上Toast不显示的解决方法

Android 5.0以上Toast不显示的解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

甜性涩爱3gp,ca922航班,陈怡芬博客

原因分析

用户使用android 5.0以上的系统在安装app时,将消息通知的权限关闭掉了。实际上用户本意只是想关闭notification,但是toast的show方法中有调用inotificationmanager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。

toast.show()

效果图

自定义toast(上)与toast(下)比对

问题解决

既然系统不允许我们调用toast,那么我们就自立门户——自己写一个toast出来。我们总体的思路是:在activity的布局中添加view实现toast的效果。

toast背景shape定义

我们知道shape的背景是一个半透明黑色的圆角效果:


toast

因此我们使用的是shape的corners和solid结点:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="#99000000" />
 <corners android:radius="8dp" />
</shape>

定义布局

布局中我们主要使用textview控件,设置相应的边距和背景即可,布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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="200dp"
 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:background="@drawable/shape_toastutils_bg"
  android:gravity="bottom|center"
  android:orientation="vertical"
  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:layout_weight="1"
   android:gravity="center"
   android:shadowcolor="#bb000000"
   android:shadowradius="2.75"
   android:textsize="12sp"
   android:textcolor="#ffffffff" />
 </linearlayout>
</linearlayout>

java代码逻辑

自定义toast的java代码逻辑主要模仿系统toast的maketext() show()两个方法,此外还需要reset()方法,实现toast显示过程中activity切换时context也随之切换,关键代码如下:

maketext(context context, string message, int hide_delay)方法:

public static toastutils maketext(context context, string message,
        int hide_delay) {
 if (minstance == null) {
  minstance = new toastutils(context);
 } else {
  // 考虑activity切换时,toast依然显示
  if (!mcontext.getclass().getname().endswith(context.getclass().getname())) {
   minstance = new toastutils(context);
  }
 }
 if (hide_delay == length_long) {
  minstance.hide_delay = 2500;
 } else {
  minstance.hide_delay = 1500;
 }
 mtextview.settext(message);
 return minstance;
}

maketext(context context, int resid, int hide_delay)方法

public static toastutils maketext(context context, int resid, int hide_delay) {
 string mes = "";
 try {
  mes = context.getresources().getstring(resid);
 } catch (resources.notfoundexception e) {
  e.printstacktrace();
 }
 return maketext(context, mes, hide_delay);
}

show()方法

public void show() {
 if (isshow) {
  // 若已经显示,则不再次显示
  return;
 }
 isshow = true;
 // 显示动画
 mfadeinanimation = new alphaanimation(0.0f, 1.0f);
 // 消失动画
 mfadeoutanimation = new alphaanimation(1.0f, 0.0f);
 mfadeoutanimation.setduration(animation_duration);
 mfadeoutanimation
   .setanimationlistener(new animation.animationlistener() {
    @override
    public void onanimationstart(animation animation) {
     // 消失动画后更改状态为 未显示
     isshow = false;
    }
    @override
    public void onanimationend(animation animation) {
     // 隐藏布局,不使用remove方法为防止多次创建多个布局
     mcontainer.setvisibility(view.gone);
    }
    @override
    public void onanimationrepeat(animation animation) {
    }
   });
 mcontainer.setvisibility(view.visible);
 mfadeinanimation.setduration(animation_duration);
 mcontainer.startanimation(mfadeinanimation);
 mhandler.postdelayed(mhiderunnable, hide_delay);
}

方法调用

自定义toast的使用与系统toast类似,调用方法如下:

toastutils.maketext(context, "消息内容",toastutils.length_short).show();

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们能有所帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网