Skip to content

Commit 983ce3c

Browse files
author
YeChao
committed
YUtils
1 parent 07f4661 commit 983ce3c

File tree

8 files changed

+82
-40
lines changed

8 files changed

+82
-40
lines changed

README.md

Lines changed: 11 additions & 5 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-
最近更新 2020.12.26
7+
最近更新 2020.12.29
88

99
### How to use
1010

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

@@ -39,7 +39,8 @@ dependencies {
3939
```
4040
//初始化
4141
YUtils.init(this);
42-
//设置打印开关(可选,默认false)
42+
43+
//打印开关(可选,默认false不打印)
4344
LogUtil.setIsLog(true);
4445
```
4546

@@ -75,6 +76,7 @@ LogUtil.setIsLog(true);
7576

7677
* void closeSoftKeyboard()<br>关闭软键盘
7778

79+
* Boolean hasSim()<br>是否有sim卡 即设备是否可以拨打电话等
7880

7981
> ## 2.ToastUtil(任意线程,不重复显示,可取消).
8082
@@ -224,6 +226,10 @@ like this
224226

225227
## 更新记录
226228

229+
#### 3.1.5(2020-12-29)
230+
- 初始化校验
231+
- YUtils新增sim卡判断
232+
227233
#### 3.1.4(2020-12-26)
228234
- SpUtil优化
229235

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<application
66
android:name=".App"
77
android:allowBackup="true"
8-
android:icon="@mipmap/ic_launcher"
8+
android:icon="@mipmap/ic_logo"
99
android:label="@string/app_name"
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ class MainActivity : AppCompatActivity() {
5353
// ActivityUtil.start(MainActivity::class.java)
5454
// ActivityUtil.finish(this)
5555

56-
LogUtil.i("" + DisplayUtil.getStatusBarHeight() + "---" + DisplayUtil.getScreenHeight())
57-
LogUtil.i("" + DisplayUtil.getActionBarHeight() + "---" + DisplayUtil.getNavBarHeight())
56+
btn_display.setOnClickListener {
57+
LogUtil.i("" + DisplayUtil.getStatusBarHeight() + "---" + DisplayUtil.getScreenHeight())
58+
LogUtil.i("" + DisplayUtil.getActionBarHeight() + "---" + DisplayUtil.getNavBarHeight())
59+
}
60+
61+
btn_sim.setOnClickListener {
62+
ToastUtil.show(if (YUtils.hasSim()) "有sim卡" else "无sim卡")
63+
}
5864
}
5965
}
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
6+
android:gravity="center"
7+
android:orientation="vertical"
78
tools:context=".MainActivity">
89

910
<TextView
1011
android:id="@+id/text_view"
1112
android:layout_width="wrap_content"
1213
android:layout_height="wrap_content"
13-
android:text="Hello World!"
14-
app:layout_constraintBottom_toBottomOf="parent"
15-
app:layout_constraintLeft_toLeftOf="parent"
16-
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent" />
14+
android:text="Hello World!" />
1815

1916
<Button
2017
android:id="@+id/button"
2118
android:layout_width="wrap_content"
2219
android:layout_height="wrap_content"
23-
android:text="存StringSet"
24-
app:layout_constraintEnd_toEndOf="parent"
25-
app:layout_constraintStart_toStartOf="parent"
26-
app:layout_constraintTop_toBottomOf="@+id/text_view" />
20+
android:text="存StringSet" />
2721

2822
<Button
2923
android:id="@+id/button2"
3024
android:layout_width="wrap_content"
3125
android:layout_height="wrap_content"
32-
android:text="读StringSet"
33-
app:layout_constraintEnd_toEndOf="parent"
34-
app:layout_constraintStart_toStartOf="parent"
35-
app:layout_constraintTop_toBottomOf="@+id/button" />
26+
android:text="读StringSet" />
3627

3728
<Button
3829
android:id="@+id/showLoading"
3930
android:layout_width="wrap_content"
4031
android:layout_height="wrap_content"
41-
android:text="showLoading"
42-
app:layout_constraintEnd_toEndOf="@+id/button2"
43-
app:layout_constraintStart_toStartOf="@+id/button2"
44-
app:layout_constraintTop_toBottomOf="@+id/button2" />
32+
android:text="showLoading" />
4533

4634
<Button
4735
android:id="@+id/hideLoading"
4836
android:layout_width="wrap_content"
4937
android:layout_height="wrap_content"
5038
android:layout_marginTop="8dp"
51-
android:text="hideLoading"
52-
app:layout_constraintEnd_toEndOf="@+id/showLoading"
53-
app:layout_constraintStart_toStartOf="@+id/showLoading"
54-
app:layout_constraintTop_toBottomOf="@+id/showLoading" />
39+
android:text="hideLoading" />
5540

5641
<Button
5742
android:id="@+id/btn_log"
5843
android:layout_width="wrap_content"
5944
android:layout_height="wrap_content"
60-
android:text="打印log"
61-
app:layout_constraintStart_toStartOf="parent"
62-
app:layout_constraintTop_toTopOf="parent" />
45+
android:text="打印log" />
6346

64-
</android.support.constraint.ConstraintLayout>
47+
<Button
48+
android:id="@+id/btn_display"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:text="打印屏幕宽高" />
52+
53+
<Button
54+
android:id="@+id/btn_sim"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:text="是否有sim卡" />
58+
59+
</LinearLayout>
5.26 KB
Loading

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<resources>
2-
<string name="app_name">app</string>
2+
<string name="app_name">YUtils</string>
33

44
<string name="large_text">
55
"Material is the metaphor.\n\n"

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.Context;
99
import android.content.pm.PackageInfo;
1010
import android.content.pm.PackageManager;
11+
import android.telephony.TelephonyManager;
1112
import android.text.Spannable;
1213
import android.text.SpannableString;
1314
import android.text.style.ForegroundColorSpan;
@@ -36,7 +37,7 @@ public class YUtils {
3637

3738
private static Toast toast;
3839
private static ProgressDialog progressDialog;
39-
private static Application mApp;
40+
private static Application mApp = null;
4041

4142
/**
4243
* 使用init()即可
@@ -62,7 +63,11 @@ public static Application getApplication() {
6263
}
6364

6465
public static Application getApp() {
65-
return mApp;
66+
if (null == mApp) {
67+
throw new NullPointerException("YUtils未在Application中初始化");
68+
} else {
69+
return mApp;
70+
}
6671
}
6772

6873
/**
@@ -267,4 +272,18 @@ public static void closeSoftKeyboard() {
267272
inputManger.hideSoftInputFromWindow(ActivityUtil.getCurrentActivity().getWindow().getDecorView().getWindowToken(), 0);
268273
}
269274

275+
/**
276+
* 是否有sim卡 即设备是否可以拨打电话等
277+
*/
278+
public static Boolean hasSim() {
279+
TelephonyManager telephonyManager = (TelephonyManager) getApp().getSystemService(Context.TELEPHONY_SERVICE);
280+
boolean result = true;
281+
switch (telephonyManager.getSimState()) {
282+
case TelephonyManager.SIM_STATE_ABSENT:
283+
case TelephonyManager.SIM_STATE_UNKNOWN:
284+
result = false;
285+
break;
286+
}
287+
return result;
288+
}
270289
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.ClipData
66
import android.content.ClipboardManager
77
import android.content.Context
88
import android.content.pm.PackageManager
9+
import android.telephony.TelephonyManager
910
import android.text.Spannable
1011
import android.text.SpannableString
1112
import android.text.style.ForegroundColorSpan
@@ -50,7 +51,11 @@ object YUtils {
5051
}
5152

5253
fun getApp(): Application {
53-
return mApp
54+
if (this::mApp.isInitialized) {
55+
return mApp
56+
} else {
57+
throw UninitializedPropertyAccessException("YUtils未在Application中初始化")
58+
}
5459
}
5560

5661
/**
@@ -234,5 +239,16 @@ object YUtils {
234239
inputManger.hideSoftInputFromWindow(ActivityUtil.currentActivity!!.window.decorView.windowToken, 0)
235240
}
236241

237-
242+
/**
243+
* 是否有sim卡 即设备是否可以拨打电话等
244+
*/
245+
fun hasSim(): Boolean {
246+
val telephonyManager = getApp().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
247+
var result = true
248+
when (telephonyManager.simState) {
249+
TelephonyManager.SIM_STATE_ABSENT -> result = false
250+
TelephonyManager.SIM_STATE_UNKNOWN -> result = false
251+
}
252+
return result
253+
}
238254
}

0 commit comments

Comments
 (0)