2017-04-12 36 views
-3

我如何知道應用程序上次在Android中使用的時間?有沒有可能確定這一點?以及如何獲取上次使用日期?如何知道何時以編程方式上次使用已安裝的應用程序?

+0

我認爲這是可能的,但我不知道該怎麼 –

+0

如果你正在開發一個應用程序,然後你可以添加所提到的功能。我不確定其他應用程序的情況。 – Nayan

+0

我不知道這正是你想要的,但你看到以下鏈接嗎? 也許可以。 http://stackoverflow.com/questions/22986542/in-android-how-can-you-get-the-last-used-open-date-of-an-application http://stackoverflow.com/questions/13205952 /如何獲取上一次打開的應用程序在Android中的日期 – tkhm

回答

0

您可以使用某些應用程序的使用UsageStatsManager

Manifest.Java

<uses-permission 
    android:name="android.permission.PACKAGE_USAGE_STATS" 
    tools:ignore="ProtectedPermissions" /> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

MainActivity.Java

的usuage
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
public class MainActivity extends Activity { 
    Button statsBtn; 
    TextView status; 
    Context mContext; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     status = (TextView) findViewById(R.id.status); 
     mContext = MainActivity.this; 
     //Check if permission enabled 
     if (UStats.getUsageStatsList(this).isEmpty()) { 
      Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); 
      startActivity(intent); 
     } 
     statsBtn = (Button) findViewById(R.id.stats_btn); 
     statsBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // UStats.printCurrentUsageStatus(MainActivity.this); 
       status.setText(UStats.printUsageStatus(mContext)); 
      } 
     }); 
    } 

} 

Ustats.Java

/** * 創建者的用戶對15年3月2日。 */

public class UStats { 
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("M-d-yyyy HH:mm:ss"); 
    public static final String TAG = UStats.class.getSimpleName(); 
    @SuppressWarnings("ResourceType") 
    public static void getStats(Context context) { 
     UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); 
     int interval = UsageStatsManager.INTERVAL_YEARLY; 
     Calendar calendar = Calendar.getInstance(); 
     long endTime = calendar.getTimeInMillis(); 
     calendar.add(Calendar.YEAR, -1); 
     long startTime = calendar.getTimeInMillis(); 
     Log.d(TAG, "Range start:" + dateFormat.format(startTime)); 
     Log.d(TAG, "Range end:" + dateFormat.format(endTime)); 
     UsageEvents uEvents = usm.queryEvents(startTime, endTime); 
     while (uEvents.hasNextEvent()) { 
      UsageEvents.Event e = new UsageEvents.Event(); 
      uEvents.getNextEvent(e); 
      if (e != null) { 
       Log.d(TAG, "Event: " + e.getPackageName() + "\t" + e.getTimeStamp()); 
      } 
     } 
    } 
    public static List<UsageStats> getUsageStatsList(Context context) { 
     UsageStatsManager usm = getUsageStatsManager(context); 
     Calendar calendar = Calendar.getInstance(); 
     long endTime = calendar.getTimeInMillis(); 
     calendar.add(Calendar.YEAR, -1); 
     long startTime = calendar.getTimeInMillis(); 
     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; 
    } 
    public void printUsageStats(List<UsageStats> usageStatsList) { 
     String value = null; 
     for (UsageStats u : usageStatsList) { 
      Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: " 
        + u.getTotalTimeInForeground()); 
      value = "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: " + u.getTotalTimeInForeground(); 
     } 
    } 
    public void printCurrentUsageStatus(Context context) { 
     printUsageStats(getUsageStatsList(context)); 
    } 
    public static String printUsageStatus(Context context) { 
     return printUsageStatss(getUsageStatsList(context)); 
    } 
    @SuppressWarnings("ResourceType") 
    private static UsageStatsManager getUsageStatsManager(Context context) { 
     UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); 
     return usm; 
    } 
    public static String printUsageStatss(List<UsageStats> usageStatsList) { 
     String value = null; 
     for (UsageStats u : usageStatsList) { 
      Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: " 
        + u.getTotalTimeInForeground()); 
      value = "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: " + u.getTotalTimeInForeground(); 
     } 
     return value; 
    } 
} 
相關問題