整个程序的入口Activity从Manifest里面可以找到是SplashscreenActivity,界面截图如下:
程序的代码如下:
public class SplashscreenActivity extends Activity { public final static String FIRST_RUN_PREFERENCE = "first_run"; private Animation endAnimation; private Handler endAnimationHandler; private Runnable endAnimationRunnable; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.splashscreen); findViewById(R.id.splashlayout); endAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out); endAnimation.setFillAfter(true); endAnimationHandler = new Handler(); endAnimationRunnable = new Runnable() { @Override public void run() { findViewById(R.id.splashlayout).startAnimation(endAnimation); } }; endAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { HomeActivity.launch(SplashscreenActivity.this); SplashscreenActivity.this.finish(); } }); showTutorial(); } final void showTutorial() { boolean showTutorial = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(FIRST_RUN_PREFERENCE, true); if (showTutorial) { final TutorialDialog dlg = new TutorialDialog(this); dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { CheckBox cb = (CheckBox) dlg.findViewById(R.id.toggleFirstRun); if (cb != null && cb.isChecked()) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SplashscreenActivity.this); prefs.edit().putBoolean(FIRST_RUN_PREFERENCE, false).commit(); } endAnimationHandler.removeCallbacks(endAnimationRunnable); endAnimationHandler.postDelayed(endAnimationRunnable, 2000); } }); dlg.show(); } else { endAnimationHandler.removeCallbacks(endAnimationRunnable); endAnimationHandler.postDelayed(endAnimationRunnable, 1500); } } }备注一:SharedPreferences的保存
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SplashscreenActivity.this);
prefs.edit().putBoolean(FIRST_RUN_PREFERENCE, false).commit();
刚开始的时候以为传入的是SplashscreenActivity,那么生成的那个xml文件的名字应该是和这个activity有关才对,手机没有root权限所以通脱调试看到不是这样的
看以看到是和整个程序的包名有关,现在明白其实这个函数的参数只要是传入的是属于这个程序的context的对象就可以。
再有就是对于何时判断实现这个对话框的判断,刚开始想在onCreate()里面判断是不是显示不是更好,后来发现这样做不好,程序的封装行就被破坏了,我要处理是不是显示对话框,onCreate()只要调用一个函数就可以了,不管你在你在里面是怎样处理的是显示还是不显示。
备注二:动画的处理
如果这个动画让我处理的话可能就是执行动画然后,执行完了跳到对应的activity中去。而在这里是用Handler和Runnable来处理的。
endAnimationHandler.removeCallbacks(endAnimationRunnable);
endAnimationHandler.postDelayed(endAnimationRunnable, 1500);
API中的解释:
void android.os.Handler.removeCallbacks(Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.
- Parameters:
- r The Runnable that will be executed.
- delayMillis The delay (in milliseconds) until the Runnable will be executed.
- Returns:
- Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
HomeActivity.launch(SplashscreenActivity.this);
SplashscreenActivity.this.finish();
在HomeActivity里面这个launch是一个静态的函数:
/** * Launch Home activity helper * * @param c context where launch home from (used by SplashscreenActivity) */ public static void launch(Context c){ Intent intent = new Intent(c, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); c.startActivity(intent); }
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );这里先留一个问题,查了一下还是没能太明白这个的准确意思
备注三:对dialog添加消失的监听事件
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {}
之前一直不知道有这么一个监听事件,以后可以在程序中使用到