// 防止第三方跳转时出现双实例
Activity aty = AppManager.getActivity(MainActivity.class);
if (aty != null && !aty.isFinishing()) {
finish();
}
防止第三方跳转的原理是:进入AppManager.class可以发现其实是一个Activity.class实例的管理类.主要负责用栈的方式管理已经存在的Activity实例.
通过AppManager.getActivity(MainActivity.class)查询是否管理类里面是否已经保存MainActivity.class的实例.如果有则将已经存在的实例finish(防止MainActivity.class双实例)
protected void onResume() {
super.onResume();
int cacheVersion = PreferenceHelper.readInt(this, "first_install",
"first_install", -1);
int currentVersion = TDevice.getVersionCode();
if (cacheVersion < currentVersion) {
PreferenceHelper.write(this, "first_install", "first_install",
currentVersion);
cleanImageCache();
}
}
private void cleanImageCache() {
final File folder = FileUtils.getSaveFolder("OSChina/imagecache");
File[] files = folder.listFiles();
if (files != null && files.length > 0) {
KJAsyncTask.execute(new Runnable() {
@Override
public void run() {
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
file.delete();
}
}
}
});
}
}
个人理解,不知道是不是这样的:onResume()中拿到版本号,判断是否是第一次运行。
如果是第一次运行,就清空OSChina/imagecache里面的文件,cleanImageCache()方法就是用来清空缓存的。
private void redirectTo() {
Intent uploadLog = new Intent(this, LogUploadService.class);
startService(uploadLog);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
开启一个服务,用来查看ErrorLog,并上报给服务器。
至于Appmanager和LogUploadService这两个类,没仔细研究,太复杂了,对我这个初学者来说,有点难度。