2017-07-25 9 views
0

私はAndroid Appを作成しています。私は、アプリの起動時に実行される何らかの声のメッセージを欲しい。私はすでにテキスト部分をテキストを使って考え出しましたが、アプリのstartup.Canで自動的に実行することはできません。誰かが私を助けますか?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> 
関連する問題