55import  android .content .Context ;
66import  android .content .Intent ;
77import  android .os .Bundle ;
8+ import  android .os .Handler ;
89import  android .support .v7 .app .AppCompatActivity ;
910import  android .view .View ;
1011import  android .widget .TextView ;
1516
1617public  class  MainActivity  extends  AppCompatActivity  {
1718
19+ 
1820    @ Override 
1921    protected  void  onCreate (Bundle  savedInstanceState ) {
2022        super .onCreate (savedInstanceState );
2123        setContentView (R .layout .activity_main );
22-         findViewById (R .id .btn1 ).setOnClickListener (new  View .OnClickListener () {
23-             @ Override 
24-             public  void  onClick (View  v ) {
25-                 OneKeyPerm .request (getApplication (), Manifest .permission .CAMERA , "您需要允许相机权限,否则无法使用扫码功能" , new  OneKeyPerm .OnPermResultListener () {
26-                     @ Override 
27-                     public  void  onPermResult (String  perm , boolean  isGrant ) {
28-                         Toast .makeText (MainActivity .this , "请求相机权限 "  + isGrant , Toast .LENGTH_SHORT ).show ();
29-                     }
30-                 },true );
31-             }
24+         findViewById (R .id .btn1 ).setOnClickListener (v  -> requestCameraPermission ());
25+ 
26+         findViewById (R .id .btn2 ).setOnClickListener (v  -> requestStoragePermission ());
27+ 
28+         findViewById (R .id .btn3 ).setOnClickListener (v  -> {
29+             showHomeScreen ();
30+             new  Handler ().postDelayed (() -> requestCameraPermission (), 2000 );
3231        });
3332
34-         findViewById (R .id .btn2 ).setOnClickListener (new  View .OnClickListener () {
35-             @ Override 
36-             public  void  onClick (View  v ) {
37-                 OneKeyPerm .request (getApplication (), Manifest .permission .READ_EXTERNAL_STORAGE , "您需要允许读取文件权限,否则无法查看图片" , new  OneKeyPerm .OnPermResultListener () {
38-                     @ Override 
39-                     public  void  onPermResult (String  perm , boolean  isGrant ) {
40-                         Toast .makeText (MainActivity .this , "请求读取权限 "  + isGrant , Toast .LENGTH_SHORT ).show ();
41-                     }
42-                 },true );
43-             }
33+         findViewById (R .id .btn4 ).setOnClickListener (v  -> {
34+             showHomeScreen ();
35+             new  Handler ().postDelayed (() -> requestStoragePermission (), 2000 );
4436        });
4537
46-         findViewById (R .id .newAct ).setOnClickListener (new  View .OnClickListener () {
47-             @ Override 
48-             public  void  onClick (View  v ) {
49-                 startActivity (new  Intent (getApplicationContext (), SecondAct .class ));
50-             }
38+         findViewById (R .id .btn5 ).setOnClickListener (v  -> {
39+             finish ();
40+             new  Handler ().postDelayed (() -> requestCameraPermission (), 2000 );
5141        });
42+ 
43+         findViewById (R .id .btn6 ).setOnClickListener (v  -> {
44+             finish ();
45+             new  Handler ().postDelayed (() -> requestStoragePermission (), 2000 );
46+         });
47+ 
48+ 
49+         findViewById (R .id .newAct ).setOnClickListener (v  -> startActivity (new  Intent (getApplicationContext (), SecondAct .class )));
5250        ((TextView ) findViewById (R .id .tv )).setText (processName () + " "  + android .os .Process .myPid ());
5351
5452    }
5553
54+     private  void  requestStoragePermission () {
55+         OneKeyPerm .request (getApplication (), Manifest .permission .READ_EXTERNAL_STORAGE , "您需要允许读取文件权限,否则无法查看图片" , new  OneKeyPerm .OnPermResultListener () {
56+             @ Override 
57+             public  void  onPermResult (String  perm , boolean  isGrant ) {
58+                 Toast .makeText (MainActivity .this , "请求读取权限 "  + isGrant , Toast .LENGTH_SHORT ).show ();
59+             }
60+         }, true );
61+     }
62+ 
63+     private  void  showHomeScreen () {
64+         Intent  intent  = new  Intent (Intent .ACTION_MAIN );
65+         intent .addCategory (Intent .CATEGORY_HOME );
66+         intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
67+         startActivity (intent );
68+     }
69+ 
5670    private  String  processName () {
5771        int  pid  = android .os .Process .myPid ();
5872        String  processName ;
@@ -65,4 +79,13 @@ private String processName() {
6579        }
6680        return  null ;
6781    }
82+ 
83+     private  void  requestCameraPermission () {
84+         OneKeyPerm .request (getApplication (), Manifest .permission .CAMERA , "您需要允许相机权限,否则无法使用扫码功能" , new  OneKeyPerm .OnPermResultListener () {
85+             @ Override 
86+             public  void  onPermResult (String  perm , boolean  isGrant ) {
87+                 Toast .makeText (MainActivity .this , "请求相机权限 "  + isGrant , Toast .LENGTH_SHORT ).show ();
88+             }
89+         }, true );
90+     }
6891}
0 commit comments