2016-07-05 129 views

回答

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); 
} 

上面的代碼將打開一個訪問設置頁面,以使我們的應用程序,以獲得其他應用程序使用狀態(一次)。

希望這會有所幫助。

相關問題