有什麼方法可以獲取當前運行或打開的android應用程序的列表,以便我可以找到設備中運行的最後一個應用程序。謝謝Android:獲取當前打開的應用程序的堆棧(數組)
1
A
回答
1
我正在快速工作幾天的表情符號鍵盤。我需要三天時間才能獲取前臺應用程序包名稱以發送表情符號圖像,因爲每當我想發送圖像時,它都會彈出intentchooser
以選擇其中一個可將圖像作爲額外處理的應用程序。
所有教程和鏈接對我來說都不起作用,因爲Google不贊成使用getRunningTasks()
方法來獲取當前正在運行的應用程序。
然後我有一個絕妙的想法,使用ResolveInfo
獲得設備上當前運行的應用程序的堆棧,但它沒有用。
最後,我在API 21中引入了一個名爲UsageStatsManager
的新類,它適用於我。 This github link提供如何使用此類來獲取正在運行的應用程序包名。
下面是我的代碼是怎麼在上面運行的程序包名的應用程序:
public class UStats {
public static final String TAG = UStats.class.getSimpleName();
@SuppressLint("SimpleDateFormat")
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("M-d-yyyy HH:mm:ss");
public static ArrayList<String> printCurrentUsageStatus(Context context) {
return printUsageStats(getUsageStatsList(context));
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static ArrayList<String> printUsageStats(List<UsageStats> usageStatsList) {
HashMap<String, Integer> lastApp = new HashMap<String, Integer>();
for (UsageStats u : usageStatsList) {
lastApp.put(u.getPackageName(), (int) u.getLastTimeStamp());
/*Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: "
+ u.getTotalTimeInForeground() + "\t" + "LastTimeStamp: " + new Date(u.getLastTimeStamp()));*/
}
Map<String, Integer> sortedMapAsc = sortByComparator(lastApp);
ArrayList<String> firstApp = new ArrayList<>();
for (Map.Entry<String, Integer> entry : sortedMapAsc.entrySet()) {
String key = entry.getKey();
//Integer value = entry.getValue();
firstApp.add(key);
/*System.out.println("package name: " + key + ", time " + new Date(Math.abs(value)));*/
}
return firstApp;
}
// To check the USAGE_STATS_SERVICE permission
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<UsageStats> getUsageStatsList(Context context) {
UsageStatsManager usm = getUsageStatsManager(context);
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.MINUTE, -1);
long startTime = calendar.getTimeInMillis();
Log.d(TAG, "Under getUsageStateList");
Log.d(TAG, "Range start:" + dateFormat.format(startTime));
Log.d(TAG, "Range end:" + dateFormat.format(endTime));
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
return usageStatsList;
}
// Sort the map in the ascending order of the timeStamp
private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) {
List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet());
// Sorting the list based on values
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
// Maintaining insertion order with the help of LinkedList
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
@SuppressWarnings("ResourceType")
private static UsageStatsManager getUsageStatsManager(Context context) {
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
return usm;
}
}
然後,我只是得到了應用包名稱的ArrayList中,使用下面的代碼:
ArrayList<String> sortedApplication = UStats.printCurrentUsageStatus(SimpleIME.this);
Log.d("TAG", "applicationList: " + sortedApplication.toString());
不要忘了添加權限:
<uses-permission android:name = "android.permission.PACKAGE_USAGE_STATS"
tools:ignore = "ProtectedPermissions"/>
和下面的代碼來檢查我們的應用程序,以獲得其他應用程序狀態的權限:
if (UStats.getUsageStatsList(this).isEmpty()) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
Toast.makeText(MainActivity.this, "Enable Usage Access for YOUR_APP_NAME to use this app", Toast.LENGTH_LONG).show();
startActivity(intent);
}
上面的代碼將打開一個訪問設置頁面,以使我們的應用程序,以獲得其他應用程序使用狀態(一次)。
希望這會有所幫助。
相關問題
- 1. 擴展程序獲取當前打開的應用程序
- 2. 從命令行獲取當前打開的應用程序
- 3. 如何以編程方式獲取當前位於android的最近堆棧中的應用程序的名稱?
- 4. Android應用程序堆棧視圖
- 5. 如何獲取應用程序分配的堆的當前數量?
- 6. 如何從rails中的應用程序獲取堆棧跟蹤?
- 7. Linux:獲取程序當前打開的文件列表
- 8. 獲取Android應用程序的當前上下文
- 9. 獲取當前的緯度和經度在android應用程序
- 10. 獲取當前打開的文檔
- 11. 如何在OpenGL中獲取矩陣堆棧的當前大小?
- 12. 如何獲得當前堆棧級別
- 13. 如何獲取當前android應用程序佔用的數據量?
- 14. 如何在android工作室中打開應用程序之前獲取通知
- 15. Branch.io - 打開應用程序但未獲取數據的鏈接
- 16. 無法從java應用程序連接到打開的堆棧對象存儲
- 17. 從香柏木堆棧上的heroku python應用程序打開ssh隧道?
- 18. 如何在android應用程序中獲取當前位置
- 19. 如何打破Cocoa Touch應用程序中的「導航堆棧」?
- 20. 看到應用程序線程使用的堆棧和堆量
- 21. 獲取當前前臺應用程序的名稱
- 22. 內核堆棧與用戶模式應用程序堆棧
- 23. 當擴展程序打開時,從Chrome中的當前選項卡獲取URL?
- 24. 打開Android應用程序的問題
- 25. 如何從應用程序活動堆棧中分離Android應用程序小部件活動堆棧?
- 26. 如何獲得使用C#的堆棧中的當前索引#
- 27. Linux - 獲取線程的堆棧內存的開始和結束
- 28. 正確的堆棧爲開發Android本地實時聲音應用程序
- 29. 當應用程序不應該打開時最大化應用程序打開
- 30. 堆棧到Android應用程序中的圖像