有没有办法以编程方式确定上次打开应用程序的时间?

问题描述 投票:3回答:1

我想知道上次打开应用程序(即Facebook或WhatsApp)的时间。

没有root,有没有办法以编程方式确定Android上次打开应用的时间?

重要信息:

我之前提出的一个问题是,如果我保留一些日志变量,那么如果在我的Android应用程序安装之前使用了应用程序,那么我将无法确定上次使用该应用程序的时间。

java android usage-statistics
1个回答
1
投票

来自谷歌示例:

https://github.com/googlesamples/android-AppUsageStatistics

public List<UsageStats> getUsageStatistics(int intervalType) {
        // Get the app statistics since one year ago from the current time.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);

        List<UsageStats> queryUsageStats = mUsageStatsManager
                .queryUsageStats(intervalType, cal.getTimeInMillis(),
                        System.currentTimeMillis());

        if (queryUsageStats.size() == 0) {
            Log.i(TAG, "The user may not allow the access to apps usage. ");
            Toast.makeText(getActivity(),
                    getString(R.string.explanation_access_to_appusage_is_not_enabled),
                    Toast.LENGTH_LONG).show();
            mOpenUsageSettingButton.setVisibility(View.VISIBLE);
            mOpenUsageSettingButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
                }
            });
        }
        return queryUsageStats;
    }

他们你可以使用这个:

http://developer.android.com/intl/es/reference/android/app/usage/UsageStats.html#getLastTimeUsed()

if( mUsageStats.getPackageName().equals("YOUR PACKAGE NAME")){
    mUsageStats.getLastTimeUsed();
}
© www.soinside.com 2019 - 2024. All rights reserved.