2017-07-25 106 views
0

我正在創建一個Android應用程序。我想要一些語音信息,當應用程序啓動時會執行這些信息。我已經使用TEXT TO SPEECH計算出了消息部分,但無法在應用程序啓動時自動執行。可以有人幫我嗎?如何在我的Android應用程序啓動時執行某些操作

+1

您是否嘗試過使用主要活動的onCreate方法? – JamesSwinton

+0

將主要活動代碼放在這裏,我們會更好地幫助您 –

+0

取決於「應用程序啓動」的含義,如果您正在討論第一次啓動,請使用Application類。如果您在第一次啓動時使用onPostCreate時正在討論何時輸入特定活動,如果您每次進入活動時都使用onStart或onResume。 –

回答

0

如果你想一次執行代碼wehn應用程序啓動那麼這可能是一個可能的解決方案: 公共類MyApp的擴展應用{

public MyApp() { 
     // this method fires only once per application start. 
     // getApplicationContext returns null here 

     Log.i("main", "Constructor fired"); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate();  

     // this method fires once as well as constructor 
     // but also application has context here 

     Log.i("main", "onCreate fired"); 
    } 
} 

然後你要註冊這個類作爲內部AndroidManifest您的應用程序類。 xml

<application android:label="@string/app_name" android:name=".MyApp"> <------- here 
    <activity android:name="MyActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 
</application> 
相關問題