Jawaher Ali
Jawaher Ali

Reputation: 11

function which implemented once in android app

is there any function which implemented once after first installation of an android app? since my application is voice renegotiation app and I want to give the user instructions by voice after opening in the first time?

Upvotes: 1

Views: 123

Answers (4)

Siddharth Lele
Siddharth Lele

Reputation: 27748

Short answer:

No.

A slightly longer answer:

Android does not provide a built in mechanism for you to handle such tasks. It does however, provide you the mechanism to so.

Read up about SharedPreferences here.

Sample:

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(); 

And to retrieve the value from the SharePreference:

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

now check what the value of the blnTutorial is:

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

Upvotes: 0

Maikel Bollemeijer
Maikel Bollemeijer

Reputation: 6575

You are looking for SharedPreferences. Take a this tutorial and find out how they work. Once you know how this works, you know how to do the thing you want.

Its very important to read about this, because you will use this tech in almost every app you are gonna make in the future.

Hope this helps.

Upvotes: 1

B770
B770

Reputation: 1292

You can do this with sharedPreferences. (http://developer.android.com/reference/android/content/SharedPreferences.html or http://developer.android.com/guide/topics/data/data-storage.html) e.g.

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();
}

Upvotes: 0

rciovati
rciovati

Reputation: 28063

There is no built-in function to do that but you can easy achieve it using SharedPreferences.

For instance, in your Activity, you can read a preference in this way:

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

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

Once you've done with your setup update the preference value:

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

More about SharedPreferences:

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

Upvotes: 0

Related Questions