2013-04-28 88 views
1

有没有在第一次安装Android应用程序后实现一次的功能? 由于我的应用程序是语音重新协商应用程序,我想在第一次打开后通过语音给用户指示?在Android应用程序中实现一次的功能

+0

在我看来,最简单的方法是使用'SharedPreference' – hardartcore 2013-04-28 08:21:30

回答

1

您正在寻找SharedPreferences。 参考本教程,了解它们的工作原理。 一旦你知道这是如何工作,你知道如何做你想要的东西。

对于阅读这篇文章非常重要,因为您几乎可以在将来要制作的所有应用程序中使用该技术。

希望这会有所帮助。

0

简短的回答:

稍长的答案:

Android不提供内置的机制,为您处理这些任务。但是,它确实为您提供了这样的机制。

阅读关于SharedPreferences here

样品:

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("SOME_FILE_NAME", Context.MODE_PRIVATE); 

// PUT THIS AFTER THE INSTRUCTIONS/TUTORIAL IS DONE PLAYING 
Editor editor = sharedPrefs.edit(); 
editor.putBoolean("TUTORIAL_SHOWN", true); 

// DO NOT SKIP THIS. IF YOU DO SKIP, THE VALUE WILL NOT BE RETAINED BEYOND THIS SESSION 
editor.commit(); 

,并检索从SharePreference值:

boolean blnTutorial = extras.getBoolean("TUTORIAL_SHOWN", false); 

现在检查一下blnTutorial的值是:

if (blnTutorial == false) { 
    // SHOW THE TUTORIAL 
} else { 
    // DON'T SHOW THE TUTORIAL AGAIN 
} 
0

有没有内置的功能,但y ou可以使用SharedPreferences轻松实现。

例如,在你的活动,你可以看到这样一个偏好:

SharedPreferences settings = getSharedPreferences("my_preferences", 0); 
boolean setupDone = settings.getBoolean("setup_done", false); 

if (!setupDone) { 
    //Do what you need 
} 

一旦你与你的设置进行更新喜好值:

SharedPreferences.Editor editor = settings.edit(); 
editor.putBoolean("setup_done", true); 
editor.commit(); 

更多SharedPreferences

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

0

你可以用sharedPreferences来做到这一点。 (http://developer.android.com/reference/android/content/SharedPreferences.htmlhttp://developer.android.com/guide/topics/data/data-storage.html) 例如

SharedPreferences settings= getSharedPreferences(PREFS_NAME, 0); 
boolean first_run= settings.getBoolean("first", true); 

if(first_run){ 
///show instruction 
SharedPreferences.Editor editor = settings.edit(); 
editor.putBoolean("first", false); 
editor.commit(); 
} 
相关问题