Skip to content

Commit a75729f

Browse files
author
Your Name
committed
v1.1
1 parent 606ed8a commit a75729f

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ android {
1919
minSdk 21
2020
targetSdk 33
2121
versionCode 1
22-
versionName "1.0"
22+
versionName "1.1"
2323
vectorDrawables {
2424
useSupportLibrary true
2525
}
@@ -52,5 +52,5 @@ android {
5252
}
5353

5454
dependencies {
55-
55+
implementation 'androidx.appcompat:appcompat:1.3.1'
5656
}

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5+
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
6+
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
7+
tools:ignore="ProtectedPermissions" />
58
<uses-permission
69
android:name="android.permission.SET_TIME_ZONE"
710
tools:ignore="ProtectedPermissions" />

app/src/main/java/com/niostack/adbext/MainActivity.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package com.niostack.adbext;
22

3+
import android.Manifest;
4+
import android.annotation.SuppressLint;
35
import android.app.Activity;
46
import android.app.AlarmManager;
57
import android.content.Context;
68
import android.content.Intent;
9+
import android.content.pm.PackageManager;
710
import android.content.res.Configuration;
11+
import android.os.Build;
812
import android.os.Bundle;
13+
import android.telephony.SubscriptionInfo;
14+
import android.telephony.SubscriptionManager;
15+
import android.telephony.TelephonyManager;
916
import android.webkit.WebView;
1017
import android.widget.TextView;
1118
import android.widget.Toast;
1219

20+
import androidx.annotation.NonNull;
21+
import androidx.core.app.ActivityCompat;
22+
import androidx.core.content.ContextCompat;
23+
1324
import java.lang.reflect.Field;
1425
import java.lang.reflect.Method;
1526
import java.util.Locale;
@@ -25,13 +36,74 @@
2536
* adb shell am start -n com.niostack.adbext/.MainActivity --es language zh
2637
*/
2738
public class MainActivity extends Activity {
39+
private final int REQUEST_PHONE_STATE_PERMISSION = 123;
40+
2841
@Override
2942
protected void onCreate(Bundle savedInstanceState) {
3043
super.onCreate(savedInstanceState);
3144
setContentView(R.layout.activity_main);
45+
// 检查是否已经获得了READ_PHONE_STATE权限
46+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
47+
!= PackageManager.PERMISSION_GRANTED) {
48+
// 如果没有获得权限,请求权限
49+
ActivityCompat.requestPermissions(this,
50+
new String[]{Manifest.permission.READ_PHONE_STATE},
51+
REQUEST_PHONE_STATE_PERMISSION);
52+
} else {
53+
// 已经获得权限,可以执行需要该权限的操作
54+
// 在此处调用获取MCC和MNC的代码
55+
System.out.println("已经获得权限,可以执行需要该权限的操作");
56+
setMCCMNC();
57+
}
3258
setView();
3359
}
34-
private void setView(){
60+
61+
private void setMCCMNC() {
62+
String mccmnc = getMccMnc(this, 0);
63+
TextView MCCMNC = findViewById(R.id.MCCMNC);
64+
MCCMNC.setText(mccmnc);
65+
}
66+
67+
// 处理权限请求的回调
68+
@Override
69+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
70+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
71+
72+
if (requestCode == REQUEST_PHONE_STATE_PERMISSION) {
73+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
74+
// 用户授予了READ_PHONE_STATE权限,可以执行需要该权限的操作
75+
// 在此处调用获取MCC和MNC的代码
76+
System.out.println("用户授予了READ_PHONE_STATE权限");
77+
setMCCMNC();
78+
} else {
79+
// 用户拒绝了权限请求,可以在这里处理拒绝的情况
80+
System.out.println("用户拒绝了权限请求");
81+
}
82+
}
83+
}
84+
85+
// 获取MCC和MNC
86+
public String getMccMnc(Context context, int subscriptionId) {
87+
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
88+
89+
if (telephonyManager != null) {
90+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
91+
SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
92+
SubscriptionInfo subscriptionInfo = subscriptionManager.getActiveSubscriptionInfo(subscriptionId);
93+
94+
if (subscriptionInfo != null) {
95+
String mccMnc = subscriptionInfo.getMccString() + subscriptionInfo.getMncString();
96+
return mccMnc;
97+
}
98+
} else {
99+
String mccMnc = telephonyManager.getSimOperator();
100+
return mccMnc;
101+
}
102+
}
103+
104+
return null;
105+
}
106+
private void setView() {
35107
TextView et_current_timezone = findViewById(R.id.et_current_timezone);
36108
TextView et_current_language = findViewById(R.id.et_current_language);
37109
WebView tv_use_instructions_content = findViewById(R.id.tv_use_instructions_content);
@@ -41,6 +113,7 @@ private void setView(){
41113
//获取当前手机系统时区设置
42114
String timeZone = TimeZone.getDefault().getID();
43115
et_current_timezone.setText(timeZone);
116+
44117
//使用说明
45118

46119
String use_instructions_content = getString(R.string.use_instructions_content);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@
5151
android:layout_height="wrap_content"
5252
android:padding="16dp" />
5353
</LinearLayout>
54+
<!--MCCMNC-->
55+
<LinearLayout
56+
android:layout_width="match_parent"
57+
android:layout_height="wrap_content"
58+
android:orientation="horizontal">
59+
60+
<TextView
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:gravity="center_horizontal"
64+
android:padding="16dp"
65+
android:text="MCCMNC"
66+
android:textSize="20sp" />
67+
68+
<TextView
69+
android:id="@+id/MCCMNC"
70+
android:layout_width="match_parent"
71+
android:layout_height="wrap_content"
72+
android:padding="16dp"/>
73+
</LinearLayout>
5474
<!--使用说明-->
5575
<TextView
5676
android:layout_width="match_parent"

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<p><b>本工具提供了通过 adb 命令修改系统语言和时区的功能,使用方式如下:</b></p>
1010
<p><b>第一步:授权本工具的修改系统设置权限</b></p>
1111
<p><code>adb shell pm grant com.niostack.adbext android.permission.CHANGE_CONFIGURATION</code></p>
12+
<p><code>adb shell pm grant com.niostack.adbext android.permission.READ_PRIVILEGED_PHONE_STATE</code></p>
1213
<p><b>第二步:输入要修改的语言或时区</b></p>
1314
命令示例(修改语言为中文):
1415
<p><code>adb shell am start -n com.niostack.adbext/.MainActivity --es language zh</code></p>

0 commit comments

Comments
 (0)