我想知道Android中应用程序的最近启动时间?
答案 0 :(得分:1)
您可以在应用程序的主活动的onCreate中记录系统时间。然后可以在类级别字段中设置此时间,并从应用程序的其他位置访问。 你也可以为onResume(..)等提供类似的变量
public class MainActivity extends Activity {
long launchTime;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
launchTime = System.currentTimeMillis();
....
}
}
答案 1 :(得分:0)
您可以做的最好的事情是创建一个跟踪所有正在运行的应用程序的STICKY服务。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run()
{
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < services.size(); i++) {
if(!stalkList.contains(services.get(i).baseActivity.getPackageName()))
{
// you may store the time here
stalkList.add(services.get(i).baseActivity.getPackageName());
}
}
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++) {
ArrayList<String> runningPkgs = new ArrayList<String>(Arrays.asList(procInfos.get(i).pkgList));
Collection diff = subtractSets(runningPkgs, stalkList);
if(diff != null)
{
stalkList.removeAll(diff);
}
}
}
}, 20000, 6000); // every 6 seconds
return START_STICKY;
}