4

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.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
Salman
  • 195
  • 1
  • 7

4 Answers4

8

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);
    }
erad
  • 1,766
  • 2
  • 17
  • 26
  • 1
    I'm developing my app using ApacheCordova, With JavaScript. it seems your code is written on JAVA or something else – Salman Sep 13 '14 at 15:49
6

I added a field to the localstorage and on startup just check if that field exists. So something like this:

if (window.localStorage.getItem("installed") == undefined) {
   /* run function */
   window.localStorage.setItem("installed", true);
}

Edit: The reason I prefer this over the other methods is that this works on iOS, WP, etc as well, and not only on android

muuk
  • 932
  • 1
  • 7
  • 15
0

This should be what u are searching for:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) 
{
// run your one time code
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
0

Use some boolean value if its true don't call that function other wise call that function example is here

if(smssent!=true)
{

//call sms sending method

}


else

{

//just leave blank or else notify user using some toast message

}

Note:-the boolean value store in some database like sharedprefernce or sqllite, files....

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
Dondaldo
  • 56
  • 5