Windows 10 Android Studio latest version Java
** This method worked fine on my smartphone as of 02/27/2020. ** ** ** There may be specification changes, so if you copy and paste it and it doesn't work, please refer to the official document. ** **
I needed to get how many other apps were running that day. For example, the image below is an app called ActionDash, which shows the launch times of other apps.
In conclusion, you can use UsageStatsManager.
UsageStatsManager
First, write it in ʻAndroidManifest.xml` as below.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="****">
<!--Added the two lines below-->
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<application>
<!--abridgement-->
</application>
</manifest>
Next, perform a permission check. Use a special method instead of the usual permission method.
private boolean checkReadStatsPermission() {
//Get AppOpsManager
AppOpsManager aom = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
// GET_USAGE_Get STATS status
int mode = aom.checkOp(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), getPackageName());
if (mode == AppOpsManager.MODE_DEFAULT) {
//If the AppOps status is the default, perform a normal permission check.
//False for ordinary apps
return checkPermission("android.permission.PACKAGE_USAGE_STATS", android.os.Process.myPid(), android.os.Process.myUid()) == PackageManager.PERMISSION_GRANTED;
}
//Only allowed is true if the AppOps state is not the default
return mode == AppOpsManager.MODE_ALLOWED;
}
Finally, there is a request for permission. Please note that this is also different from the usual method.
if (!checkReadStatsPermission()) {
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
}
The explanation is included in the source code. This source code gets the app usage time for the day.
public class UsageStatsClass {
// Log.d()And the name to identify it as the output of this class
private static final String TAG = UsageStatsClass.class.getSimpleName();
//Substitute Context of MainActivity
private Context context;
public UsageStatsClass(Context mContext) {
//Substitute context
context = mContext;
}
//Object called UsageStats is information of one application(App usage time, etc.)Is included
//That is, one Usage Stats is assigned to each app.
private List<UsageStats> getUsageStatsObject() {
// getSystemService()To get UsageStatsManager
//UsageStatsManager is for getting usage information of the application
UsageStatsManager usageStatsManager =
(UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
//Get the current time as a Calendar object
Calendar calendar = Calendar.getInstance();
//Set the calendar time to midnight
//As a result, the time information contained in the calendar will be from the current time to today's midnight.
calendar.set(Calendar.HOUR_OF_DAY, 0);
// queryUsageStats(Unit of time to get,The beginning of the time to get, the end of the time to get)
//Unit of time to get:Daily(INTERVAL_DAILY), Weekly(INTERVAL_WEEKLY), Monthly(INTERVAL_MONTHLY)、
//Yearly(INTERVAL_YEARLY), Automatic selection(INTERVAL_BEST)There is
//
//The beginning of time to get:The starting point of the time zone of the data you want to acquire. This time, it's midnight of the day.
//End of time to get:The end of the time zone for the data you want to retrieve. This time, the current time.
return usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_DAILY,
calendar.getTimeInMillis(),
System.currentTimeMillis());
}
//Function to execute from the outside
public void readOneDayUsageStats() {
//Get usage information for each app as a List
List<UsageStats> usageStats = getUsageStatsObject();
//Get usage information of one app in usageStat by using for statement
for (UsageStats usageStat : usageStats) {
//If you have never used the app, skip it
if (usageStat.getTotalTimeInForeground() == 0) {
continue;
}
//Output the acquired information with Logcat
// package name : getPackageName() :The app's unique ID
// total time displayed : getTotalTimeInForeground() :Total time the app was displayed on the screen
// first time : getFirstTimeStamp() :Returns the start time of the acquired data in milliseconds
// getStringDate()Is used to convert milliseconds into a human-friendly form.
// end time : getLastTimeUsed() :Returns the end time of the retrieved data in milliseconds
// getStringDate()Is used to convert milliseconds into a human-friendly form.
Log.d(TAG, "packageName: " + usageStat.getPackageName() + "\ttotalTimeDisplayed: " + usageStat.getTotalTimeInForeground()
+ "\tfirstTime: " + getStringDate(usageStat.getFirstTimeStamp()) + "\tlastTime: " + getStringDate(usageStat.getLastTimeUsed()));
}
}
//Convert long type milliseconds to String type human-friendly form
private String getStringDate(long milliseconds) {
final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.JAPANESE);
final Date date = new Date(milliseconds);
return df.format(date);
}
}
The time is displayed for each app. You did it!
D/UsageStatsClass: packageName: com.huawei.android.launcher totalTimeDisplayed: 3769989 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 15:59:00
D/UsageStatsClass: packageName: jp.naver.line.android totalTimeDisplayed: 805413 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 15:34:36
D/UsageStatsClass: packageName: com.discord totalTimeDisplayed: 4247 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 15:43:05
D/UsageStatsClass: packageName: com.microsoft.office.outlook totalTimeDisplayed: 43011 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 14:19:16
D/UsageStatsClass: packageName: com.google.android.packageinstaller totalTimeDisplayed: 2444 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 15:59:02
D/UsageStatsClass: packageName: com.google.android.apps.photos totalTimeDisplayed: 283917 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 13:38:33
D/UsageStatsClass: packageName: com.spotify.music totalTimeDisplayed: 6267989 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 13:56:21
D/UsageStatsClass: packageName: jp.mineo.app.phone totalTimeDisplayed: 70175 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 13:59:50
D/UsageStatsClass: packageName: com.google.android.apps.translate totalTimeDisplayed: 8170 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 15:04:14
D/UsageStatsClass: packageName: ch.bitspin.timely totalTimeDisplayed: 798142 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 11:17:25
D/UsageStatsClass: packageName: com.android.settings totalTimeDisplayed: 21715 firstTime: 2020/02/27 00:00:02 lastTime: 2020/02/27 14:32:32
Recommended Posts