Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function that fires only once after app is installed on phone

I'm developing a mobile app using ApacheCordova/Phonegap. I need a function that sends a SMS to me once per install. If I put my function on "DeviceReady" it will be run each time the app opens. Is there any solution for a function to be run when app is installed OR when it runs for first time?

Any suggestion would be appreciated.

like image 721
Salman Avatar asked Dec 09 '22 06:12

Salman


1 Answers

Check if it is the first time with a method and then perform the action if that method determines that it is the first time.

Ex:

isFirstTime() Method

private boolean isFirstTime()
    {
    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean ranBefore = preferences.getBoolean("RanBefore", false);
    if (!ranBefore) {

        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("RanBefore", true);
        editor.commit();
        // Send the SMS

        }
    return ranBefore;

    }

You may want to add it to your onCreate()

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    topLevelLayout = findViewById(R.id.top_layout);



   if (isFirstTime()) {
        topLevelLayout.setVisibility(View.INVISIBLE);
    }
like image 116
erad Avatar answered Dec 11 '22 10:12

erad