2013-08-27 49 views

回答

10

我會建議你使用SharedPreferences爲:

的基本想法是,你讀了SharedPreferences並尋找起初不啓動應用程序存在有一個布爾值。通過 默認情況下,如果找不到 的值,則返回「true」,表示它實際上是第一次啓動應用程序。然後,在第一次啓動應用程序之後,您將在SharedPreferences中存儲值 「false」,並且在下次啓動時,將從SharedPreferences中讀取值 「false」,表示它不再是第一個應用程序啓動時的 。

這裏是它如何可能看起來像一個例子:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // your other code... 
    // setContentView(...) initialize drawer and stuff like that... 

    // use thread for performance 
    Thread t = new Thread(new Runnable() { 

     @Override 
     public void run() { 

      SharedPreferences sp = Context.getSharedPreferences("yoursharedprefs", 0); 
      boolean isFirstStart = sp.getBoolean("key", true); 
      // we will not get a value at first start, so true will be returned 

      // if it was the first app start 
      if(isFirstStart) { 
       mDrawerLayout.openDrawer(mDrawerList); 
       Editor e = sp.edit(); 
       // we save the value "false", indicating that it is no longer the first appstart 
       e.putBoolean("key", false); 
       e.commit(); 
      } 
     }   
    }); 

    t.start(); 
} 
相關問題