11package  com .pyy .signin ;
22
33import  android .app .Activity ;
4- import  android .app .AlertDialog ;
4+ import  android .app .AppOpsManager ;
55import  android .content .Context ;
6- import  android .content .DialogInterface ;
76import  android .content .Intent ;
87import  android .content .pm .PackageManager ;
8+ import  android .os .Build ;
99import  android .os .Bundle ;
10- import  android .os .Looper ;
1110import  android .provider .Settings ;
12- import  android .support .annotation .NonNull ;
13- import  android .util .Log ;
14- import  android .view .Gravity ;
15- import  android .view .LayoutInflater ;
1611import  android .view .View ;
17- import  android .view .ViewGroup ;
18- import  android .widget .ArrayAdapter ;
1912import  android .widget .Button ;
20- import  android .widget .CheckBox ;
21- import  android .widget .CompoundButton ;
22- import  android .widget .ImageView ;
2313import  android .widget .ListView ;
24- import  android .widget .TextView ;
25- import  android .widget .Toast ;
26- 27- import  java .io .DataOutputStream ;
2814import  java .util .ArrayList ;
29- import  java .util .concurrent .ThreadFactory ;
30- import  java .util .concurrent .TimeUnit ;
3115import  java .util .concurrent .locks .Condition ;
3216import  java .util .concurrent .locks .Lock ;
3317import  java .util .concurrent .locks .ReentrantLock ;
@@ -43,11 +27,15 @@ public class MainPage extends Activity {
4327 static  Lock  lock  = new  ReentrantLock ();
4428 static  Condition  condition  = lock .newCondition ();
4529 static  boolean  flag  = false ;
30+  private  static  final  int  REQUEST_PACKAGE_USAGE_STATS_PERMISSION  = 1010 ;
4631
4732 @ Override 
4833 protected  void  onCreate (Bundle  savedInstanceState ) {
4934 super .onCreate (savedInstanceState );
5035 setContentView (R .layout .activity_main );
36+  if  (Build .VERSION .SDK_INT  >= Build .VERSION_CODES .LOLLIPOP ) {
37+  checkPermission (REQUEST_PACKAGE_USAGE_STATS_PERMISSION );
38+  }
5139 Utils .checkSignInServiceEnabled (this );
5240 appListView  = findViewById (R .id .appListView );
5341 Button  start  = findViewById (R .id .button );
@@ -60,6 +48,30 @@ public void onClick(View arg0) {
6048 });
6149 }
6250
51+  private  void  checkPermission (int  requestCode ) {
52+  if  (requestCode  == REQUEST_PACKAGE_USAGE_STATS_PERMISSION ) {
53+  if  (!hasPermission ()) {
54+  //如果用户没有开启"可访问使用记录",则跳转到该设置项并提醒用户打开 
55+  Utils .showToast ("\" 签到助手\" 提示:\n 请开启\" 可访问使用记录\" 权限" , this );
56+  startActivityForResult (
57+  new  Intent (Settings .ACTION_USAGE_ACCESS_SETTINGS ),
58+  REQUEST_PACKAGE_USAGE_STATS_PERMISSION );
59+  }
60+  }
61+  }
62+ 63+  //检测用户是否对本app开启了"Apps with usage access"权限 
64+  private  boolean  hasPermission () {
65+  AppOpsManager  appOps  = (AppOpsManager )
66+  getSystemService (Context .APP_OPS_SERVICE );
67+  int  mode  = 0 ;
68+  if  (Build .VERSION .SDK_INT  > Build .VERSION_CODES .KITKAT ) {
69+  mode  = appOps .checkOpNoThrow (AppOpsManager .OPSTR_GET_USAGE_STATS ,
70+  android .os .Process .myUid (), getPackageName ());
71+  }
72+  return  mode  == AppOpsManager .MODE_ALLOWED ;
73+  }
74+ 6375 class  SignInThread  extends  Thread  {
6476 @ Override 
6577 public  void  run () {
@@ -92,7 +104,6 @@ private void autoSignIn(AppInfo info) {
92104 try  {
93105 autoLaunch (info .packageName );
94106 condition .await ();
95- 96107 } catch  (InterruptedException  e ) {
97108 e .printStackTrace ();
98109 }
0 commit comments