Create a function that works only once after the application is installed on the phone

I am developing a mobile application using ApacheCordova / Phonegap. I need a function that sends me an SMS once per installation. If I put my function on "DeviceReady", it will run every time the application opens. Is there any solution for the function that will be launched when the application is installed or when it is executed for the first time?

Any suggestion would be appreciated.

+4
source share
4 answers

localstorage, , . - :

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

: , , , iOS, WP .., android

+6

, , , , .

:

isFirstTime()

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;

    }

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

, :

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

, ,

if(smssent!=true)
{

//call sms sending method

}


else

{

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

}

: , sharedprefernce sqllite, ....

0

All Articles