Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yechaoa/YUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
yechao committed Oct 16, 2021
2 parents 8ef6dbf + 5793fdf commit fffa415
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 17 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![](https://img.shields.io/badge/language-Java%20&%20kotlin-orange.svg)
![](https://img.shields.io/hexpm/l/plug.svg)

最近更新 2021.01.26
最近更新 2021.05.25

### How to use

Expand All @@ -22,13 +22,13 @@ allprojects {
```
dependencies {
//kotlin 版本
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.0'
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.1'
//java 版本
implementation 'com.github.yechaoa.YUtils:yutils:3.2.0'
implementation 'com.github.yechaoa.YUtils:yutils:3.2.1'
//kotlin && java 版本
implementation 'com.github.yechaoa:YUtils:3.2.0'
implementation 'com.github.yechaoa:YUtils:3.2.1'
}
```

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

* Application getApp()<br>获取全局上下文
* Application getApp()<br>获取Application

* Context getAppContext()<br>获取全局上下文

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

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

* void cancel()<br>取消Toast,onDestroy时调用,或onPause,当前页面finish之后在下一个页面不会再显示


> kotlin分支中添加了一个Toast扩展,比之前使用更加简单方便,调用示例:<br>
> "111".show() -> 111 <br>
> 111.show() -> 111 <br>
> 111.show("222") -> 222
> ## 3.LogUtil(日志打印,带方法行数链接,可超长打印)
Expand Down Expand Up @@ -224,36 +230,16 @@ like this

<br>

## 更新记录
## 近期更新

#### 3.2.1(2021-05-25)
- 优化调整
- 添加Toast扩展

#### 3.2.0(2021-01-26)
- targetSdkVersion 30
- ToastUtil适配Android 11

#### 3.1.5(2020-12-29)
- 初始化校验
- YUtils新增sim卡判断

#### 3.1.4(2020-12-26)
- SpUtil优化

#### 3.1.3(2020-10-30)
- 添加DisplayUtil
- 优化。

#### 3.1.2 (2020-10-22)
优化。

#### 3.1.1
优化引用方式,可以kotlin和java分开引用,也可以一起引用。

#### 3.1.0
简化了部分util原有的调用方式,属于兼容更新。

#### 3.0.0
- 3.0.0以后新增了`kotlin`版本;
- 此外,java版本和kotlin版本都删除了`ExitUtils`
- kotlin版本没有`GsonUtil`


### 混淆
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/yechaoa/app/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.yechaoa.app

import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.yechaoa.app.databinding.ActivityMainBinding
import com.yechaoa.yutilskt.*
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {

private lateinit var mBinding: ActivityMainBinding

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

mBinding.button.setOnClickListener {
SpUtil.setStringSet("testStringSet", set)
//SpUtil.getBoolean("111",true)
}

mBinding.button2.setOnClickListener {
Expand Down Expand Up @@ -64,5 +68,17 @@ class MainActivity : AppCompatActivity() {
mBinding.btnSim.setOnClickListener {
ToastUtil.show(if (YUtils.hasSim()) "有sim卡" else "无sim卡")
}

mBinding.btnThread.setOnClickListener {
LogUtil.d("" + mainLooper.isCurrentThread)
LogUtil.d("" + Looper.myLooper()!!.isCurrentThread)
val b = mainLooper == Looper.myLooper()
LogUtil.d("" + b)
thread {
ToastUtil.show("213")
}

}

}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@
android:layout_height="wrap_content"
android:text="是否有sim卡" />

<Button
android:id="@+id/btn_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打印线程" />

</LinearLayout>
9 changes: 5 additions & 4 deletions yutils/src/main/java/com/yechaoa/yutils/ToastUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yechaoa.yutils;

import android.annotation.SuppressLint;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

Expand All @@ -23,7 +24,7 @@ public class ToastUtil {
*/
@Deprecated
public static void showToast(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand All @@ -41,7 +42,7 @@ public void run() {
* @param msg 需要显示的参数
*/
public static void show(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand Down Expand Up @@ -75,7 +76,7 @@ private static void createToast(String msg) {
*/
@Deprecated
public static void showCenterToast(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand All @@ -93,7 +94,7 @@ public void run() {
* @param msg 需要显示的参数
*/
public static void showCenter(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand Down
6 changes: 5 additions & 1 deletion yutils/src/main/java/com/yechaoa/yutils/YUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ public static Application getApplication() {

public static Application getApp() {
if (null == mApp) {
throw new NullPointerException("YUtils未在Application中初始化");
throw new NullPointerException("YUtils is not initialized in application");
} else {
return mApp;
}
}

public static Context getAppContext() {
return getApp().getApplicationContext();
}

/**
* 获取屏幕宽度 改为DisplayUtil.getScreenWidth()
*/
Expand Down
14 changes: 14 additions & 0 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.yechaoa.yutilskt

import android.widget.Toast

/**
* Created by yechao on 2021/5/25.
* Describe : Toast 扩展
* "111".show() -> 111
* 111.show() -> 111
* 111.show("222") -> 222
*/
fun Any.show(msg: String = this.toString(), duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(YUtils.getAppContext(), msg, duration).show()
}
15 changes: 8 additions & 7 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yechaoa.yutilskt

import android.annotation.SuppressLint
import android.os.Looper
import android.view.Gravity
import android.widget.Toast

Expand All @@ -19,17 +20,17 @@ object ToastUtil {
*
* @param msg 需要显示的参数
*/
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtilKt.show(msg)"))
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtil.show(msg)"))
fun showToast(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg)
} else {
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
}
}

fun show(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg)
} else {
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
Expand All @@ -56,17 +57,17 @@ object ToastUtil {
*
* @param msg 需要显示的参数
*/
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtilKt.showCenter(msg)"))
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtil.showCenter(msg)"))
fun showCenterToast(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
}
}

fun showCenter(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
Expand Down Expand Up @@ -94,7 +95,7 @@ object ToastUtil {
* onDestroy时调用,或者onPause
* 当前页面finish之后在下一个页面不会显示
*/
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtilKt.cancel()"))
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtil.cancel()"))
fun cancelToast() {
toast?.cancel()
}
Expand Down
9 changes: 7 additions & 2 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ object YUtils {
if (this::mApp.isInitialized) {
return mApp
} else {
throw UninitializedPropertyAccessException("YUtils未在Application中初始化")
throw UninitializedPropertyAccessException("YUtils is not initialized in application")
}
}

fun getAppContext(): Context {
return getApp().applicationContext
}

/**
* 获取屏幕宽度
*/
Expand Down Expand Up @@ -235,7 +239,8 @@ object YUtils {
* 关闭软键盘
*/
fun closeSoftKeyboard() {
val inputManger = ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputManger =
ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManger.hideSoftInputFromWindow(ActivityUtil.currentActivity!!.window.decorView.windowToken, 0)
}

Expand Down

0 comments on commit fffa415

Please sign in to comment.