Skip to content

Commit d1e95dd

Browse files
author
YeChao
committed
优化调整
1 parent 61eede4 commit d1e95dd

File tree

12 files changed

+82
-52
lines changed

12 files changed

+82
-52
lines changed

.idea/compiler.xml

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

.idea/misc.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/modules.xml

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

.idea/runConfigurations.xml

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

README.md

Lines changed: 12 additions & 6 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扩展,比之前使用更加简单方便,调用示例:
92+
> "111".show() -> 111
93+
> 111.show() -> 111
94+
> 111.show("222") -> 222
8995
9096
> ## 3.LogUtil(日志打印,带方法行数链接,可超长打印)
9197

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.yechaoa.app
22

3+
import android.os.Build
34
import android.os.Bundle
4-
import androidx.appcompat.app.AppCompatActivity
5+
import android.os.Looper
56
import android.widget.TextView
6-
import com.google.android.material.snackbar.Snackbar
7+
import androidx.annotation.RequiresApi
8+
import androidx.appcompat.app.AppCompatActivity
79
import com.yechaoa.yutilskt.*
810
import kotlinx.android.synthetic.main.activity_main.*
11+
import kotlin.concurrent.thread
912

1013

1114
class MainActivity : AppCompatActivity() {
1215

16+
@RequiresApi(Build.VERSION_CODES.M)
1317
override fun onCreate(savedInstanceState: Bundle?) {
1418
super.onCreate(savedInstanceState)
1519
setContentView(R.layout.activity_main)
@@ -31,7 +35,6 @@ class MainActivity : AppCompatActivity() {
3135

3236
button.setOnClickListener {
3337
SpUtil.setStringSet("testStringSet", set)
34-
//SpUtil.getBoolean("111",true)
3538
}
3639

3740
button2.setOnClickListener {
@@ -51,9 +54,6 @@ class MainActivity : AppCompatActivity() {
5154
LogUtil.i("" + ActivityUtil.currentActivityName)
5255
}
5356

54-
// ActivityUtil.start(MainActivity::class.java)
55-
// ActivityUtil.finish(this)
56-
5757
btn_display.setOnClickListener {
5858
LogUtil.i("" + DisplayUtil.getStatusBarHeight() + "---" + DisplayUtil.getScreenHeight())
5959
LogUtil.i("" + DisplayUtil.getActionBarHeight() + "---" + DisplayUtil.getNavBarHeight())
@@ -62,5 +62,17 @@ class MainActivity : AppCompatActivity() {
6262
btn_sim.setOnClickListener {
6363
ToastUtil.show(if (YUtils.hasSim()) "有sim卡" else "无sim卡")
6464
}
65+
66+
btn_thread.setOnClickListener {
67+
LogUtil.d("" + mainLooper.isCurrentThread)
68+
LogUtil.d("" + Looper.myLooper()!!.isCurrentThread)
69+
val b = mainLooper == Looper.myLooper()
70+
LogUtil.d("" + b)
71+
thread {
72+
ToastUtil.show("213")
73+
}
74+
75+
}
76+
6577
}
6678
}

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+
}

0 commit comments

Comments
 (0)