随笔
1、判断是否第一次启动
代码示例:
/**
* 如果当前版本大于上次版本,该版本属于第一次启动
* 将当前版本写入preference中,则下次启动的时候,据此判断,不再为首次启动
*/
private void redirectTo() {
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("pageName", 0);
int currentVersion = info.versionCode;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
int lastVersion = prefs.getInt("KEY", 0);
if (currentVersion > lastVersion) {
prefs.edit().putInt("KEY", currentVersion).commit();
Intent intent = new Intent(this, SlidePageViewActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2、创建快捷方式
代码示例:
private void setUpShortCut() {
// TODO Auto-generated method stub
Intent intent = new Intent("com.android.launcher.action.ADD_SHORTCUT");
// 设置快捷方式图片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// 设置是否允许重复创建快捷方式 false表示不允许
intent.putExtra("duplicate", false);
Intent targetIntent = new Intent();
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory("android.intent.category.LAUNCHER");
targetIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //activity的一种启动方式。如果不存在则创建。如果存在则重用
ComponentName componentName = new ComponentName(getPackageName(), this.getClass().getName());
targetIntent.setComponent(componentName);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
// 发送广播
sendBroadcast(intent);
}
如需删除快捷方式,戳这里查看