android - How to use UsageStatsManager? -


background

Google has disliked the function "" in the "ActivityManager" class, now all those Getting the list of apps that the current app has opened.

I have also written a post about it, but I saw that it is impossible.

The problem

I have created a post about it (and, similarly, created by someone else), and requested it to reconsider, and Google A new class has decided to create similar functionality (like more data, but it can also be useful), but I can not know how to use it.

The class is called "UsageStatsManager", and my guess is that the function "works"

In addition, it seems that this is a new permission ("android.permission .PACKAGE_USAGE_STATS "), which is a system permit, but it is written that.

Declaration of permission means intent to use APIs and the user's user settings can allow the application through the application.

Another link about this new functionality

I've found

I have seen on the code o f android, and found that "context" USAGE_STATS_SERVICE, next in Javadocs, is that which is:

Use
  / ** * to retrieve a {@link * {@link #getSystemService} with Android.app.UsageStatsManager} to interact with the status bar. * * @see #getSystemService * @see android.app.UsageStatsManager * @hide * / public static final string USAGE_STATS_SERVICE = "usagestats";  

The strange thing is that not only does it say, "status bar", but it also does not match with PackageName (rather than "android.app.usage.UsageStatsManager") .

I have also added the correct permission:

    

and here are the codes I use:

  Final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService ("usagestats"); // Context.USAGE_STATS_SERVICE); Last Interval Current = Calendar. Gate Instance (). Get (Calendar .YEAR); Last list & lt; UsageStats & gt; QueryUsageStats = usageStatsManager.queryUsageStats (UsageStatsManager.INTERVAL_YEARLY, currentYear -2, currentYear); In the  emulator, I went to "Settings" -> "Security" -> "Use with Use" and enabled my app.  

However, when the code is running, I just get an empty list ...

Question

How do you use UsageStatsManager?

In addition, how do you allow the user in the easiest way to allow? Or is it done automatically, as soon as the app tries to get the required information?

What happens when trying to use this class, but the user has not yet confirmed this?

How can I return the code to a real list of apps?

I think the documentation was small hand for calendar stuff I do not think it really is just 2014 works with; Although I may be wrong

To actually reach the list of EusastStats, you have to create a calendar object with the correct month, day and year. In fact the MRK said in the second reply. I copied the errors in the code of the MRK and corrected it so that anyone could see it in the future. Can see it.

  Calendar starts Cal = Calendar.getInstance (); BeginCal.set (Calendar.Det, 1); BeginCal.set (Calendar. MONTH, 0); BeginCal.set (Calendar. IEER, 2012); Calendar Endcill = Calendar. Just Instance (); EndCal.set (calendar.date, 1); EndCal.set (Calendar. MONTH, 0); EndCal.set (Calendar. IEER, 2014); Last list & lt; UsageStats & gt; QueryUsageStats = usageStatsManager.queryUsageStats (UsageStatsManager.INTERVAL_YEARLY, beginCal.getTimeInMillis (), endCal.getTimeInMillis ());  

- credit mrk; Improved by me (she accidentally puts the call instead of starting the bus and the end time)

The code for the access access setting is below :)

  intent to intent = new Intent (settings.action_UNACACASS_STTEGS); StartActivity;  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -