Android more than 5.0 toast does not show the solution _android

Source: Internet
Author: User

Cause analysis

Users using Android more than 5.0 of the system when the app is installed, the permission to notify the message off. In fact, the user simply wants to close notification, but Toast's Show method has a call to Inotificationmanager, which is disabled when the user closes the message notification permission, so our toast cannot be displayed.

Toast.show ()

Effect chart

Custom Toast (upper) vs toast (bottom)

Problem solving

Since the system does not allow us to call toast, then we do it on our own-write a toast ourselves. Our overall idea is to add a view to the activity layout to achieve the toast effect.

Toast background Shape definition

We know that the background of shape is a translucent black rounded corner effect:


Toast

So we are using the corners and solid nodes of shape:

<?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>

Define Layout

In the layout we mainly use the TextView control, set the corresponding margins and background, the layout code is as follows:

<?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:shadow Color= "#BB000000" android:shadowradius= "2.75" Android:teXtsize= "12SP" android:textcolor= "#FFFFFFFF"/> </LinearLayout> </LinearLayout> 

Java Code Logic

The custom Toast Java code logic mainly imitates the system toast makeText() , show() two methods, moreover also needs reset() the method, realizes toast the activity switch when the activity switches the context also to switch, the key code is as follows:

makeText(Context context, String message, int HIDE_DELAY)Method:

public static Toastutils Maketext (context, String,
        int hide_delay) {
 if (minstance = = null) {
  Minstance = new Toastutils (context);
 else {
  //Considering the activity switch, toast still displays
  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)Method

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()Method

public void Show () {
 if (isshow) {
  //If it is already displayed, the return is not displayed again
  ;
 }
 Isshow = true;
 Display animation
 mfadeinanimation = new Alphaanimation (0.0f, 1.0f);
 Vanishing animation
 mfadeoutanimation = new Alphaanimation (1.0f, 0.0f);
 Mfadeoutanimation.setduration (animation_duration);
 Mfadeoutanimation
   . Setanimationlistener (New Animation.animationlistener () {
    @Override public
    Void Onanimationstart (Animation Animation) {
     //Vanishing animation After change status is not displayed
     Isshow = false;
    }
    @Override public
    void Onanimationend (Animation Animation) {
     //Hide layout, do not use the Remove method to prevent multiple layouts
     from being created multiple times 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);
}

Method calls

The use of custom toast is similar to the system toast, and the method is invoked as follows:

Toastutils.maketext (context, "message content", Toastutils.length_short). Show ();

Summarize

This is the full content of this article, I hope the content of this article for your Android developers can help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.