2016-07-31 74 views
4

我的應用程序需要具有使用訪問權限才能獲取用戶手機上當前正在運行的應用程序的相關信息。我可以在以下鏈接的幫助下使用以下代碼成功實現它。如何以編程方式獲取使用訪問權限

Usage Access apps

這是我工作的代碼

public void showDialog() 
{ 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 

     @SuppressWarnings("WrongConstant") 
     UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats"); 
     long time = System.currentTimeMillis(); 
     List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 
       time - 1000 * 1000, time); 
     if (appList.size()==0) 
     { 
      AlertDialog alertDialog = new AlertDialog.Builder(this) 
        .setTitle("Usage Access") 
        .setMessage("App will not run without usage access permissions.") 
        .setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
         @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
         public void onClick(DialogInterface dialog, int which) { 
          // continue with delete 
          Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); 
          // intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$SecuritySettingsActivity")); 
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivityForResult(intent,0); 
         } 
        }) 
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // do nothing 
          dialog.dismiss(); 
         } 
        }) 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .create(); 

      alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
      alertDialog.show(); 
     }      else { 
      Intent intent = new Intent(this, PackageService.class); 
      startService(intent); 
      finish(); 
     } 
    }else 
    { 
     Intent intent = new Intent(this, PackageService.class); 
     startService(intent); 
     finish(); 
    } 
} 

該代碼會顯示用戶的警告對話框,使用訪問權限是必需的,並會採取用戶的設置,從那裏可以啓用篩選。

一切都與此完美合作。只有我想要的是如何給我的應用程序使用訪問權限默認情況下不顯示任何對話框給用戶,直到他/她沒有手動關閉它?

+0

代碼中有你找到任何解決辦法請爲此? –

+0

@vivek,一旦它打開,你是如何檢測當前在用戶手機中打開的應用程序..?我的意思是PackageService的詳細信息 –

+0

@Anukoolsrivastav你能描述一下你想問什麼嗎? –

回答

8

你可以簡單地給這個權限的清單,忽略眼前這個權限錯誤:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>

不是使用經過許可

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (!isAccessGranted()) { 
     Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); 
     startActivity(intent); 
    } 
} 


private boolean isAccessGranted() { 
     try { 
      PackageManager packageManager = getPackageManager(); 
      ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0); 
      AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); 
      int mode = 0; 
      if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.KITKAT) { 
       mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, 
         applicationInfo.uid, applicationInfo.packageName); 
      } 
      return (mode == AppOpsManager.MODE_ALLOWED); 

     } catch (PackageManager.NameNotFoundException e) { 
      return false; 
     } 
    } 
+0

是否適用於棉花糖或更高? –

+0

是的,它適用於那個@ĜüpåŠhãsĥwæt –

+0

thnx,它對我很有用 –

相關問題