Last active
August 29, 2015 14:22
-
-
Save wasnot/d27764ffd5c7b3b23179 to your computer and use it in GitHub Desktop.
[Android][Lollipop]UsageStatsManagerを使ってみた ref: http://qiita.com/wasnot/items/6074dbcdd49141f8e7d8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* implementation of PkgUsageStats associated with an | |
* application package. | |
* @hide | |
*/ | |
public class PkgUsageStats implements Parcelable { | |
public String packageName; | |
public int launchCount; | |
public long usageTime; | |
public Map<String, Long> componentResumeTimes; | |
public PkgUsageStats(String pkgName, int count, long time, Map<String, Long> lastResumeTimes) { | |
packageName = pkgName; | |
launchCount = count; | |
usageTime = time; | |
componentResumeTimes = new HashMap<String, Long>(lastResumeTimes); | |
} | |
// Parcelableの実装があります。。 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// クラス名 | |
getClassName() | |
// パッケージ名 | |
getPackageName() | |
// イベントのタイムスタンプ | |
getTimeStamp() | |
// イベントのタイプ. MOVE_TO_FOREGROUND/MOVE_TO_BACKGROUND/CONFIGURATION_CHANGE/NONE | |
getEventType() | |
// Configuration. イベントタイプがCONFIGURATION_CHANGEのときのみ返す | |
getConfiguration() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 集計期間の開始時刻 | |
getFirstTimeStamp() | |
// 集計期間の終了時刻 | |
getLastTimeStamp() | |
// 最後に実行された時刻 | |
getLastTimeActive() | |
// 期間内に実行された回数 | |
getActivationCount() | |
// 期間内に実行された合計時間 | |
getTotalTimeActive() | |
// Configuration | |
getConfiguration() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 集計期間の開始時刻 | |
getFirstTimeStamp() | |
// 集計期間の終了時刻 | |
getLastTimeStamp() | |
// 最後に実行された時刻 | |
getLastTimeUsed() | |
// 期間内に全面で実行された合計時間 | |
getTotalTimeInForeground() | |
// パッケージ名 | |
getPackageName() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 期間が1日なのに、intervalTypeを年単位にしてしまった | |
int intervalType = UsageStatsManager.INTERVAL_YEARLY; | |
long start = System.currentTimeMillis() - 24 * 60 * 60 * 1000; | |
long end = System.currentTimeMillis(); | |
// ついてませんが、@Nullableです | |
List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats(intervalType, start, end); | |
: | |
usageStatsList.get(0).getLastTimeStamp(); | |
// 1430907218507 | |
// firstTimeStampは大丈夫 | |
usageStatsList.get(0).getFirstTimeStamp(); | |
// 2295981492 | |
// 変に丸められてる? | |
usageStatsList.get(1).getLastTimeStamp(); | |
// 1430907218507 | |
usageStatsList.get(1).getFirstTimeStamp(); | |
// 999981492 | |
// 変に丸められてる? | |
usageStatsList.get(2).getLastTimeStamp(); | |
// 1430907218507 | |
usageStatsList.get(2).getFirstTimeStamp(); | |
// 1433218181823 | |
// 大丈夫なときもある | |
: | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// パッケージ名 | |
PkgUsageStats.packageName -> UsageStats.getPackageName() | |
// 期間内の合計利用時間 | |
PkgUsageStats.usageTime -> UsageStats.getTotalTimeInForeground() | |
// 期間内の起動回数 | |
PkgUsageStats.launchCount -> なし | |
// 期間内のActivityごとの起動回数 | |
PkgUsageStats.componentResumeTimes -> なし |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ActivityManagerServiceからはこんな風に呼ばれていました。 | |
mUsageStatsService = IUsageStats.Stub.asInterface(ServiceManager.getService("usagestats")); | |
// リフレクションではこう呼んだり。。ServiceLocatorは上記ブログ参照。。 | |
mUsageStatsService = ServiceLocator.getServiceStub("usagestats", | |
"com.android.internal.app.IUsageStats$Stub"); | |
// 関数のコールもリフレクション。。 | |
Class<?> clazz = Class.fromName("com.android.internal.app.IUsageStats$Stub"); | |
Method method = clazz.getDeclaredMethod("getAllPkgUsageStats"); | |
method.setAccessible(true); | |
PkgUsageStats[] result = (PkgUsageStats[]) method.invoke(mUsageStatsService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NOTE: The last few minutes of the event log will be truncated to prevent abuse by applications. | |
// 注:イベントログの最後の数分間は、アプリケーションによって濫用を防ぐために切り捨てられます。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment