Skip to content

Commit

Permalink
兼容华为三星等机型关闭通知栏权限后吐司弹不出来的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed Nov 7, 2018
1 parent cb41b24 commit f0e3f9a
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 155 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
> 已投入公司项目多时,没有任何毛病,可胜任任何需求,[点击此处下载Demo](https://raw.githubusercontent.com/getActivity/ToastUtils/master/ToastUtils.apk)
> 想了解实现原理的可以点击此链接查看:[XToast](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/XToast.java)[ToastUtils](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/ToastUtils.java) 源码
> 想了解实现原理的可以点击此链接查看:[ToastUtils](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/ToastUtils.java) 源码
![](ToastUtils.gif)

#### 集成步骤

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

#### 初始化Toast

//建议在Application中初始化
ToastUtils.init(getApplicationContext());
// 在Application中初始化
ToastUtils.init(this);

#### 显示Toast

Expand All @@ -39,6 +39,8 @@

#### 框架亮点

* 无需权限:不管有没有授予通知栏权限都不影响吐司的弹出

* 功能强大:不分主次线程都可以弹出Toast,自动区分资源id和int类型

* 使用简单:只需传入文本,会自动根据文本长度决定吐司显示的时长
Expand All @@ -57,8 +59,20 @@

* 框架兼容性良好:本框架不依赖任何第三方库,支持Eclipse和Studio的集成使用

#### 关于通知栏权限

> 本框架已经完美解决这个问题
![](issue_taobao.gif)

![](issue_utils.gif)

#### Android技术讨论Q群:78797078

#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:

![](pay_ali.png) ![](pay_wechat.png)

## License

```text
Expand Down
Binary file modified ToastUtils.apk
Binary file not shown.
Binary file modified ToastUtils.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 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 25
versionName "2.5"
versionCode 30
versionName "3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -23,7 +23,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.hjq:titlebar:3.2'
implementation 'com.hjq:titlebar:3.5'

// implementation 'com.blankj:utilcode:1.21.0'
// implementation 'me.drakeet.support:toastcompat:1.1.0'
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hjq.toast.demo">

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
android:name=".MyApplication"
android:name=".ToastApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<activity android:name=".ToastActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import com.hjq.toast.style.ToastQQStyle;
import com.hjq.toast.style.ToastWhiteStyle;

public class MainActivity extends AppCompatActivity {
public class ToastActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_toast);
}

public void show1(final View v) {
Expand Down Expand Up @@ -51,6 +51,18 @@ public void show6(View v) {
}

public void show7(View v) {
ToastUtils.show(this);
ToastUtils.show(ToastUtils.isNotificationEnabled(this));
}

@Override
protected void onRestart() {
super.onRestart();
// 如果通知栏的权限被手动关闭了
if (!ToastUtils.isNotificationEnabled(this) && "XToast".equals(ToastUtils.getToast().getClass().getSimpleName())) {
// 因为吐司只有初始化的时候才会判断通知权限有没有开启,根据这个通知开关来显示原生的吐司还是兼容的吐司
ToastUtils.init(getApplication());
recreate();
ToastUtils.show("检查到你手动关闭了通知权限,现在只能通过重启应用,吐司才能正常显示出来");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import com.hjq.toast.ToastUtils;

public class MyApplication extends Application {
public class ToastApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
//初始化吐司
ToastUtils.init(getApplicationContext());
ToastUtils.init(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context=".ToastActivity">

<com.hjq.bar.TitleBar
android:layout_width="match_parent"
Expand All @@ -18,7 +18,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -78,9 +77,9 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="show7"
android:text="打印当前Activity对象" />
android:text="判断当前是否有通知栏权限" />
</LinearLayout>

</FrameLayout>

</LinearLayout>
</LinearLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/layout/toast_custom_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
android:padding="10dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher" />

<TextView
Expand All @@ -18,5 +18,5 @@
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
android:textSize="16sp" />
</LinearLayout>
Binary file added issue_taobao.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added issue_utils.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 25
versionName "2.5"
versionCode 30
versionName "3.0"
}
}

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

import android.app.Application;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
* author : HJQ
* github : https://github.com/getActivity/ToastUtils
* time : 2018/11/03
* desc : Toast基类
*/
abstract class BaseToast extends Toast implements Runnable {

// 显示延迟时间,避免重复点击
static final int SHOW_DELAY_MILLIS = 300;

// Toast 处理消息线程
private final Handler mHandler = new Handler(Looper.getMainLooper());

// 吐司消息View
private TextView mMessageView;

// 吐司显示的文本
private CharSequence mText;

BaseToast(Application application) {
super(application);
}

@Override
public void setView(View view) {
super.setView(view);
if (view instanceof TextView) {
mMessageView = (TextView) view; return;
}else if (view.findViewById(R.id.toast_main_text_view_id) instanceof TextView) {
mMessageView = ((TextView) view.findViewById(R.id.toast_main_text_view_id)); return;
} else if (view instanceof ViewGroup) {
if ((mMessageView = findTextView((ViewGroup) view)) != null) return;
}
// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 TextView 作为 Message View
throw new IllegalArgumentException("The layout must contain a TextView");
}

/***
* 获取当前 Handler 对象
*/
Handler getHandler() {
return mHandler;
}

/**
* 获取当前的消息 View
*/
TextView getMessageView() {
return mMessageView;
}

/**
* 获取当前欲显示的文本
*/
CharSequence getText() {
return mText;
}

@Override
public void setText(CharSequence s) {
// 记录本次吐司欲显示的文本
mText = s;
}

/**
* 递归获取ViewGroup中的TextView对象
*/
private static TextView findTextView(ViewGroup group) {
for (int i = 0; i < group.getChildCount(); i++) {
View view = group.getChildAt(i);
if ((view instanceof TextView)) {
return (TextView) view;
} else if (view instanceof ViewGroup) {
TextView textView = findTextView((ViewGroup) view);
if (textView != null) return textView;
}
}
return null;
}
}
36 changes: 0 additions & 36 deletions library/src/main/java/com/hjq/toast/DimensUtils.java

This file was deleted.

47 changes: 47 additions & 0 deletions library/src/main/java/com/hjq/toast/SupportToast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.hjq.toast;

import android.app.Application;

/**
* author : HJQ
* github : https://github.com/getActivity/ToastUtils
* time : 2018/11/02
* desc : 不需要通知栏权限的 Toast
*/
final class SupportToast extends BaseToast {

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

SupportToast(Application application) {
super(application);
mToastHelper = new ToastHelper(this, application);
}

@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();
}
}
Loading

0 comments on commit f0e3f9a

Please sign in to comment.