Kód MainActivity.java :
package eu.projectik.helloworld; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Následne do projektu pridáme ďalšie triedy, ktoré su potrebné na to, aby sa aplikácia spustila hneď po štarte telefónu. Tie si nazveme AutoStart.java a StartServiceOnBoot.java.
Kód StartServiceOnBoot.java :
package eu.projectik.helloworld; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class StartServiceOnBoot extends Service { @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { } @Override public void onStart(Intent intent, int startid) { Intent intents = new Intent(getBaseContext(),MainActivity.class); intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intents); } }
Kód AutoStart.java :
package eu.projectik.helloworld; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class AutoStart extends BroadcastReceiver { public void onReceive(Context arg0, Intent arg1) { Intent intent = new Intent(arg0,StartServiceOnBoot.class); arg0.startService(intent); } }
Následne je potrebné pridať do AndroidManifest.xml riadok :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
A výsledný AndroidManifest.xml bude vyzerať takto:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="eu.projectik.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".AutoStart" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".StartServiceOnBoot" ></service> <activity android:name=".MainActivity" 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> </manifest>