Skip to content

Commit

Permalink
新增Toast显示处理队列,修复内存泄露的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed Nov 20, 2018
1 parent f0e3f9a commit 4b8c1aa
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 222 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:toast:3.0'
implementation 'com.hjq:toast:5.0'
}

#### 初始化Toast
Expand Down Expand Up @@ -61,7 +61,9 @@

#### 关于通知栏权限

> 本框架已经完美解决这个问题
> 本框架已经完美解决这个问题(禁用通知栏权限后需要重启应用才能后生效,请以[Demo](https://raw.githubusercontent.com/getActivity/ToastUtils/master/ToastUtils.apk)测试为主)
> 具体解决方案可见:[Toast通知栏权限填坑指南](https://www.jianshu.com/p/1d64a5ccbc7c)
![](issue_taobao.gif)

Expand Down
Binary file modified ToastUtils.apk
Binary file not shown.
12 changes: 2 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 14
targetSdkVersion 26
versionCode 30
versionName "3.0"
versionCode 50
versionName "5.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -24,12 +24,4 @@ dependencies {
implementation project(':library')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.hjq:titlebar:3.5'

// implementation 'com.blankj:utilcode:1.21.0'
// implementation 'me.drakeet.support:toastcompat:1.1.0'
// implementation 'com.github.GrenderG:Toasty:1.3.0'
// implementation 'com.github.johnpersano:supertoasts:2.0'
// implementation 'com.sdsmdg.tastytoast:tastytoast:0.1.1'
// implementation 'com.muddzdev:styleabletoast:2.2.0'
// implementation 'com.github.Blincheng:EToast2:v2.1.1'
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/hjq/toast/demo/ToastActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

public void show1(final View v) {
ToastUtils.show("我是吐司");
ToastUtils.show("我是一个普通的吐司");
}

public void show2(View v) {
Expand Down Expand Up @@ -62,7 +62,7 @@ protected void onRestart() {
// 因为吐司只有初始化的时候才会判断通知权限有没有开启,根据这个通知开关来显示原生的吐司还是兼容的吐司
ToastUtils.init(getApplication());
recreate();
ToastUtils.show("检查到你手动关闭了通知权限,现在只能通过重启应用,吐司才能正常显示出来");
ToastUtils.show("检查到你手动关闭了通知权限,正在重新初始化框架,只有这样吐司才能正常显示出来");
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/toast_custom_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@id/toast_main_text_view_id"
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ android {

defaultConfig {
targetSdkVersion 26
versionCode 30
versionName "3.0"
versionCode 50
versionName "5.0"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'toast'//maven的artifact id
version = '3.0'//maven 上发布版本号
version = '5.0'//maven 上发布版本号
description = 'This is a very functional Toast'//描述,自己定义
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
}
Expand Down
91 changes: 0 additions & 91 deletions library/src/main/java/com/hjq/toast/BaseToast.java

This file was deleted.

19 changes: 2 additions & 17 deletions library/src/main/java/com/hjq/toast/SupportToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* time : 2018/11/02
* desc : 不需要通知栏权限的 Toast
*/
final class SupportToast extends BaseToast {
final class SupportToast extends XToast {

// 吐司弹窗显示辅助类
private ToastHelper mToastHelper;
private final ToastHelper mToastHelper;

SupportToast(Application application) {
super(application);
Expand All @@ -20,27 +20,12 @@ final class SupportToast extends BaseToast {

@Override
public void show() {
// 移除之前显示吐司的任务
getHandler().removeCallbacks(this);
// 添加一个显示吐司的任务
getHandler().postDelayed(this, SHOW_DELAY_MILLIS);
}

/**
* {@link Runnable}
*/
@Override
public void run() {
// 设置吐司文本
getMessageView().setText(getText());
// 显示吐司
mToastHelper.show();
}

@Override
public void cancel() {
// 移除之前显示吐司的任务
getHandler().removeCallbacks(this);
// 取消显示
mToastHelper.cancel();
}
Expand Down
106 changes: 106 additions & 0 deletions library/src/main/java/com/hjq/toast/ToastHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.hjq.toast;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/**
* author : HJQ
* github : https://github.com/getActivity/ToastUtils
* time : 2018/11/12
* desc : Toast 显示处理类
*/
final class ToastHandler extends Handler {

private static final int TYPE_SHOW = 1; // 显示吐司
private static final int TYPE_CONTINUE = 2; // 继续显示
private static final int TYPE_CANCEL = 3; // 取消显示

// 最大吐司的容量
private static final int MAX_TOAST_CAPACITY = 5;

// 吐司队列
private volatile Queue<CharSequence> mQueue = new ArrayBlockingQueue<>(MAX_TOAST_CAPACITY);

// 当前是否正在执行显示操作
private volatile boolean isShow;

// 吐司对象
private final Toast mToast;

ToastHandler(Toast toast) {
super(Looper.getMainLooper());
mToast = toast;
}

void setText(CharSequence s) {
if (mQueue.isEmpty() || !mQueue.contains(s)) {
// 添加一个元素并返回true,如果队列已满,则返回false
if (!mQueue.offer(s)) {
// 移除队列头部元素并添加一个新的元素
mQueue.poll();
mQueue.offer(s);
}
}
}

void show() {
if (!isShow) {
isShow = true;
sendEmptyMessage(TYPE_SHOW);
}
}

void cancel() {
if (isShow) {
isShow = false;
sendEmptyMessage(TYPE_CANCEL);
}
}

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TYPE_SHOW:
// 返回队列头部的元素,如果队列为空,则返回null
CharSequence text = mQueue.peek();
if (text != null) {
mToast.setText(text);
mToast.show();
// 等这个 Toast 显示完后再继续显示
sendEmptyMessageDelayed(TYPE_CONTINUE, getToastDuration(text) + 1000);
}else {
isShow = false;
}
break;
case TYPE_CONTINUE:
// 移除并返问队列头部的元素,如果队列为空,则返回null
mQueue.poll();
if (!mQueue.isEmpty()) {
sendEmptyMessage(TYPE_SHOW);
}else {
isShow = false;
}
break;
case TYPE_CANCEL:
isShow = false;
mQueue.clear();
mToast.cancel();
break;
default:
break;
}
}

/**
* 根据文本来获取吐司的显示时间
*/
private static int getToastDuration (CharSequence text) {
// 如果显示的文字超过了10个就显示长吐司,否则显示短吐司
return text.length() > 20 ? ToastHelper.LONG_DURATION_TIMEOUT : ToastHelper.SHORT_DURATION_TIMEOUT;
}
}
Loading

0 comments on commit 4b8c1aa

Please sign in to comment.