11package  com .niostack .adbext ;
22
3+ import  android .Manifest ;
4+ import  android .annotation .SuppressLint ;
35import  android .app .Activity ;
46import  android .app .AlarmManager ;
57import  android .content .Context ;
68import  android .content .Intent ;
9+ import  android .content .pm .PackageManager ;
710import  android .content .res .Configuration ;
11+ import  android .os .Build ;
812import  android .os .Bundle ;
13+ import  android .telephony .SubscriptionInfo ;
14+ import  android .telephony .SubscriptionManager ;
15+ import  android .telephony .TelephonyManager ;
916import  android .webkit .WebView ;
1017import  android .widget .TextView ;
1118import  android .widget .Toast ;
1219
20+ import  androidx .annotation .NonNull ;
21+ import  androidx .core .app .ActivityCompat ;
22+ import  androidx .core .content .ContextCompat ;
23+ 
1324import  java .lang .reflect .Field ;
1425import  java .lang .reflect .Method ;
1526import  java .util .Locale ;
2536 * adb shell am start -n com.niostack.adbext/.MainActivity --es language zh 
2637 */ 
2738public  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 );
0 commit comments