Skip to content

Commit fffa415

Browse files
author
yechao
committed
Merge branch 'master' of https://github.com/yechaoa/YUtils
2 parents 8ef6dbf + 5793fdf commit fffa415

File tree

11 files changed

+97
-47
lines changed

11 files changed

+97
-47
lines changed

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![](https://img.shields.io/badge/language-Java%20&%20kotlin-orange.svg)
55
![](https://img.shields.io/hexpm/l/plug.svg)
66

7-
最近更新 2021.01.26
7+
最近更新 2021.05.25
88

99
### How to use
1010

@@ -22,13 +22,13 @@ allprojects {
2222
```
2323
dependencies {
2424
//kotlin 版本
25-
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.0'
25+
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.1'
2626
2727
//java 版本
28-
implementation 'com.github.yechaoa.YUtils:yutils:3.2.0'
28+
implementation 'com.github.yechaoa.YUtils:yutils:3.2.1'
2929
3030
//kotlin && java 版本
31-
implementation 'com.github.yechaoa:YUtils:3.2.0'
31+
implementation 'com.github.yechaoa:YUtils:3.2.1'
3232
}
3333
```
3434

@@ -50,7 +50,9 @@ LogUtil.setIsLog(true);
5050
5151
* void init(Application app)<br>Application 中初始化
5252

53-
* Application getApp()<br>获取全局上下文
53+
* Application getApp()<br>获取Application
54+
55+
* Context getAppContext()<br>获取全局上下文
5456

5557
* void showLoading(Activity activity,String msg)<br>Loading加载框
5658

@@ -85,7 +87,11 @@ LogUtil.setIsLog(true);
8587
* void showCenter(final String msg)<br>showCenter 居中显示
8688

8789
* void cancel()<br>取消Toast,onDestroy时调用,或onPause,当前页面finish之后在下一个页面不会再显示
88-
90+
91+
> kotlin分支中添加了一个Toast扩展,比之前使用更加简单方便,调用示例:<br>
92+
> "111".show() -> 111 <br>
93+
> 111.show() -> 111 <br>
94+
> 111.show("222") -> 222
8995
9096
> ## 3.LogUtil(日志打印,带方法行数链接,可超长打印)
9197
@@ -224,36 +230,16 @@ like this
224230

225231
<br>
226232

227-
## 更新记录
233+
## 近期更新
228234

235+
#### 3.2.1(2021-05-25)
236+
- 优化调整
237+
- 添加Toast扩展
238+
229239
#### 3.2.0(2021-01-26)
230240
- targetSdkVersion 30
231241
- ToastUtil适配Android 11
232242

233-
#### 3.1.5(2020-12-29)
234-
- 初始化校验
235-
- YUtils新增sim卡判断
236-
237-
#### 3.1.4(2020-12-26)
238-
- SpUtil优化
239-
240-
#### 3.1.3(2020-10-30)
241-
- 添加DisplayUtil
242-
- 优化。
243-
244-
#### 3.1.2 (2020-10-22)
245-
优化。
246-
247-
#### 3.1.1
248-
优化引用方式,可以kotlin和java分开引用,也可以一起引用。
249-
250-
#### 3.1.0
251-
简化了部分util原有的调用方式,属于兼容更新。
252-
253-
#### 3.0.0
254-
- 3.0.0以后新增了`kotlin`版本;
255-
- 此外,java版本和kotlin版本都删除了`ExitUtils`
256-
- kotlin版本没有`GsonUtil`
257243

258244

259245
### 混淆

app/src/main/java/com/yechaoa/app/MainActivity.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.yechaoa.app
22

3+
import android.os.Build
34
import android.os.Bundle
5+
import android.os.Looper
46
import android.widget.TextView
7+
import androidx.annotation.RequiresApi
58
import androidx.appcompat.app.AppCompatActivity
69
import com.yechaoa.app.databinding.ActivityMainBinding
710
import com.yechaoa.yutilskt.*
11+
import kotlin.concurrent.thread
812

913

1014
class MainActivity : AppCompatActivity() {
1115

1216
private lateinit var mBinding: ActivityMainBinding
1317

18+
@RequiresApi(Build.VERSION_CODES.M)
1419
override fun onCreate(savedInstanceState: Bundle?) {
1520
super.onCreate(savedInstanceState)
1621
mBinding = ActivityMainBinding.inflate(layoutInflater)
@@ -33,7 +38,6 @@ class MainActivity : AppCompatActivity() {
3338

3439
mBinding.button.setOnClickListener {
3540
SpUtil.setStringSet("testStringSet", set)
36-
//SpUtil.getBoolean("111",true)
3741
}
3842

3943
mBinding.button2.setOnClickListener {
@@ -64,5 +68,17 @@ class MainActivity : AppCompatActivity() {
6468
mBinding.btnSim.setOnClickListener {
6569
ToastUtil.show(if (YUtils.hasSim()) "有sim卡" else "无sim卡")
6670
}
71+
72+
mBinding.btnThread.setOnClickListener {
73+
LogUtil.d("" + mainLooper.isCurrentThread)
74+
LogUtil.d("" + Looper.myLooper()!!.isCurrentThread)
75+
val b = mainLooper == Looper.myLooper()
76+
LogUtil.d("" + b)
77+
thread {
78+
ToastUtil.show("213")
79+
}
80+
81+
}
82+
6783
}
6884
}

app/src/main/res/layout/activity_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@
5656
android:layout_height="wrap_content"
5757
android:text="是否有sim卡" />
5858

59+
<Button
60+
android:id="@+id/btn_thread"
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:text="打印线程" />
64+
5965
</LinearLayout>

yutils/src/main/java/com/yechaoa/yutils/ToastUtil.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.yechaoa.yutils;
22

33
import android.annotation.SuppressLint;
4+
import android.os.Looper;
45
import android.view.Gravity;
56
import android.widget.Toast;
67

@@ -23,7 +24,7 @@ public class ToastUtil {
2324
*/
2425
@Deprecated
2526
public static void showToast(final String msg) {
26-
if ("main".equals(Thread.currentThread().getName())) {
27+
if (Looper.getMainLooper() == Looper.myLooper()) {
2728
createToast(msg);
2829
} else {
2930
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
@@ -41,7 +42,7 @@ public void run() {
4142
* @param msg 需要显示的参数
4243
*/
4344
public static void show(final String msg) {
44-
if ("main".equals(Thread.currentThread().getName())) {
45+
if (Looper.getMainLooper() == Looper.myLooper()) {
4546
createToast(msg);
4647
} else {
4748
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
@@ -75,7 +76,7 @@ private static void createToast(String msg) {
7576
*/
7677
@Deprecated
7778
public static void showCenterToast(final String msg) {
78-
if ("main".equals(Thread.currentThread().getName())) {
79+
if (Looper.getMainLooper() == Looper.myLooper()) {
7980
createCenterToast(msg);
8081
} else {
8182
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
@@ -93,7 +94,7 @@ public void run() {
9394
* @param msg 需要显示的参数
9495
*/
9596
public static void showCenter(final String msg) {
96-
if ("main".equals(Thread.currentThread().getName())) {
97+
if (Looper.getMainLooper() == Looper.myLooper()) {
9798
createCenterToast(msg);
9899
} else {
99100
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {

yutils/src/main/java/com/yechaoa/yutils/YUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ public static Application getApplication() {
6464

6565
public static Application getApp() {
6666
if (null == mApp) {
67-
throw new NullPointerException("YUtils未在Application中初始化");
67+
throw new NullPointerException("YUtils is not initialized in application");
6868
} else {
6969
return mApp;
7070
}
7171
}
7272

73+
public static Context getAppContext() {
74+
return getApp().getApplicationContext();
75+
}
76+
7377
/**
7478
* 获取屏幕宽度 改为DisplayUtil.getScreenWidth()
7579
*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.yechaoa.yutilskt
2+
3+
import android.widget.Toast
4+
5+
/**
6+
* Created by yechao on 2021/5/25.
7+
* Describe : Toast 扩展
8+
* "111".show() -> 111
9+
* 111.show() -> 111
10+
* 111.show("222") -> 222
11+
*/
12+
fun Any.show(msg: String = this.toString(), duration: Int = Toast.LENGTH_SHORT) {
13+
Toast.makeText(YUtils.getAppContext(), msg, duration).show()
14+
}

yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.yechaoa.yutilskt
22

33
import android.annotation.SuppressLint
4+
import android.os.Looper
45
import android.view.Gravity
56
import android.widget.Toast
67

@@ -19,17 +20,17 @@ object ToastUtil {
1920
*
2021
* @param msg 需要显示的参数
2122
*/
22-
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtilKt.show(msg)"))
23+
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtil.show(msg)"))
2324
fun showToast(msg: String) {
24-
if ("main" == Thread.currentThread().name) {
25+
if (Looper.getMainLooper() == Looper.myLooper()) {
2526
createToast(msg)
2627
} else {
2728
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
2829
}
2930
}
3031

3132
fun show(msg: String) {
32-
if ("main" == Thread.currentThread().name) {
33+
if (Looper.getMainLooper() == Looper.myLooper()) {
3334
createToast(msg)
3435
} else {
3536
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
@@ -56,17 +57,17 @@ object ToastUtil {
5657
*
5758
* @param msg 需要显示的参数
5859
*/
59-
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtilKt.showCenter(msg)"))
60+
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtil.showCenter(msg)"))
6061
fun showCenterToast(msg: String) {
61-
if ("main" == Thread.currentThread().name) {
62+
if (Looper.getMainLooper() == Looper.myLooper()) {
6263
createCenterToast(msg)
6364
} else {
6465
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
6566
}
6667
}
6768

6869
fun showCenter(msg: String) {
69-
if ("main" == Thread.currentThread().name) {
70+
if (Looper.getMainLooper() == Looper.myLooper()) {
7071
createCenterToast(msg)
7172
} else {
7273
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
@@ -94,7 +95,7 @@ object ToastUtil {
9495
* onDestroy时调用,或者onPause
9596
* 当前页面finish之后在下一个页面不会显示
9697
*/
97-
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtilKt.cancel()"))
98+
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtil.cancel()"))
9899
fun cancelToast() {
99100
toast?.cancel()
100101
}

0 commit comments

Comments
 (0)