创建一个在手机上安装应用程序后仅触发一次的功能

Sal*_*man 4 android cordova multi-device-hybrid-apps visual-studio-cordova

我正在使用ApacheCordova/Phonegap开发移动应用程序.我需要一个功能,每次安装时都会向我发送一次短信.如果我将我的功能放在"DeviceReady"上,它将在每次应用程序打开时运行.在安装应用程序或第一次运行时,是否有任何解决方案可以运行该功能?

任何建议将不胜感激.

era*_*rad 8

检查它是否是第一次使用方法,然后执行操作,如果该方法确定它是第一次.

例如:

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;

    }
Run Code Online (Sandbox Code Playgroud)

您可能想将其添加到您的 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);
    }
Run Code Online (Sandbox Code Playgroud)


muu*_*uuk 6

我在localstorage中添加了一个字段,并在启动时检查该字段是否存在.所以像这样:

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

编辑:我比其他方法更喜欢这个的原因是,这也适用于iOS,WP等,而不仅仅是在android上