2013-02-08 104 views
0

我正在實現一個具有MainActivity,onlyFirstRunActivity和alwaysRunActivity的應用程序。現在我想,上的應用程序製作安裝或更新用應用的我的應用程序週期應該是這樣的:運行活動僅適用於安裝應用程序後的第一次Android

MainActivity - > onlyFirstRunActivity - > alwaysRunActivity

安裝後或更新用我的應用程序週期應該是:

MainActivity - > alwaysRunActivity

我如何能實現這種情況

+2

SharedPreferences可以存儲一個布爾型'bFirstRun',告訴你這是否是第一次運行。 – 2013-02-08 16:50:39

+0

但最後存儲的sharedprefences仍然在updation.Means如何配置應用程序更新更新? – User42590 2013-02-08 16:57:15

+1

不確定你的意思,但是如果沒有名爲'bFirstRun'的SharedPreference ...那麼你是第一次運行。然後將其設置爲false(並提交更改)。下一次通過,它在那裏和假所以轉到'alwaysRunActivity' – 2013-02-08 17:00:18

回答

1

您可以獲取應用程序的更新時間並將其保存在SharedPreference中。

此外,你應該讓你的主要活動永遠運行一個。

//In your onCreate for your main activity/. 
if(last_update_preference < current_update_time){ 
    Update last update preference to current update time. 
    Run first activity. (Which will finish and bounce you back); 
} 

要獲得current_update_time:

How to get app install time from android

對於最後的更新時間,請參閱:

http://developer.android.com/guide/topics/data/data-storage.html#pref

+0

我認爲它只適用於更新。首先安裝什麼?並共享一個安全的方式?? – User42590 2013-02-08 16:54:56

+0

如果您返回0作爲SharedPreference的默認值,則它將用於第一次安裝,上次修改時應爲安裝時間。 (鏈接的堆棧溢出問題顯示它) – Edison 2013-02-08 16:56:04

+0

第一次last_update_preference的值是什麼? – User42590 2013-02-08 17:08:02

0

你可以嘗試這樣的事情:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    App app = (App)getApplication(); 
    if (app.getApi() != null){ 
     super.onBackPressed(); 
     startActivity(new Intent(this, MainActivity.class)); 
     return; 
    }else showLoginForm();//or do something else. 
} 
+0

我不明白你爲什麼得到app.getAp?什麼是登錄表單? – User42590 2013-02-08 17:35:45

+0

getApi()方法返回api類的對象,它根據共享首選項創建。所以這意味着開始不是第一次。我開始新的活動。或者,如果api = null表示首先是開始,並且我顯示登錄表單。 – Vetalll 2013-02-09 11:26:58

0

像這樣你可以區分新的安裝和更新。

String StoredVersionname = ""; 
    String VersionName; 
    AlertDialog LoginDialog; 
    AlertDialog UpdateDialog; 
    AlertDialog FirstRunDialog; 
    SharedPreferences prefs; 

    public static String getVersionName(Context context, Class cls) { 
       try { 
        ComponentName comp = new ComponentName(context, cls); 
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(
          comp.getPackageName(), 0); 
        return "Version: " + pinfo.versionName; 
       } catch (android.content.pm.PackageManager.NameNotFoundException e) { 
        return null; 
       } 
      } 

    public void CheckFirstRun() { 
       VersionName = MyActivity.getVersionName(this, MyActivity.class); 
prefs = PreferenceManager.getDefaultSharedPreferences(this); 
       StoredVersionname = (prefs.getString("versionname", null)); 
       if (StoredVersionname == null || StoredVersionname.length() == 0){ 
        FirstRunDialog = new FirstRunDialog(this); 
        FirstRunDialog.show(); 
       }else if (StoredVersionname != VersionName) { 
        UpdateDialog = new UpdateDialog(this); 
        UpdateDialog.show(); 
       } 
       prefs.edit().putString("versionname", VersionName).commit(); 
      } 
相關問題