我的應用程序需要具有使用訪問權限才能獲取用戶手機上當前正在運行的應用程序的相關信息。我可以在以下鏈接的幫助下使用以下代碼成功實現它。如何以編程方式獲取使用訪問權限
這是我工作的代碼
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();
}
}
該代碼會顯示用戶的警告對話框,使用訪問權限是必需的,並會採取用戶的設置,從那裏可以啓用篩選。
一切都與此完美合作。只有我想要的是如何給我的應用程序使用訪問權限默認情況下不顯示任何對話框給用戶,直到他/她沒有手動關閉它?
代碼中有你找到任何解決辦法請爲此? –
@vivek,一旦它打開,你是如何檢測當前在用戶手機中打開的應用程序..?我的意思是PackageService的詳細信息 –
@Anukoolsrivastav你能描述一下你想問什麼嗎? –