- Quick introduction to Android in general
- Platform
- Language
- VM (Dalvik -> ART)
- Play Store
- Developing apps for Android
- Tools
- Basic components
- Activities
- Fragments
- Async Tasks
- Services
- Broadcast Receivers
- Persistence
whoami
Evan Dale Aromin
- Software Engineer @ coins.ph
- Android / Python / JS
- Twitter, Github - @avendael
- avendael.com
- Aside from being a mobile device, it is a platform as well
- Platform is not only being used in phones
- Java - JDK6, recently JDK7 support added
- Trimmed down Java ie. no AWT, Swing, etc.
- Dalvik - Android's JVM equivalent
- Designed for resource constrained devices
- Uses Just In Time compilation ie. app compiled to bytecode upon launching
- ART - Android Runtime
- Replacement to Dalvik for Lollipop above
- Uses Ahead of Time compilation ie. app compiled to bytecode upon installing
- Improvements to Garbage Collection, debugging, and profiling
- Backwards compatible to Dalvik
- Google Play
- Was historically called Android Market, a place to get apps from
- Also a place for users to buy music, books, and videos
- For some countries, it is also a place where users can avail of a device
- Development tools
- Either Eclipse or Android Studio
- IntelliJ Ultimate also includes most of Android Studio features
- Android SDK
- Genymotion for emulation
- What the user primarily interacts with
- Apps are usually composed of multiple activities
- Application UI flow APIs
- Lifecycle hooks
- Lifecycle
onCreate
- Activity being created
onStart
- Activity about to become visible
onResume
- User can now interact with the Activity
onPause
- Another Activity is about to take focus
onStop
- Activity is no longer visible
onDestroy
- Activity is about to be destroyed
public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="40sp"
android:gravity="center"/>
</LinearLayout>
- Portion of UI in an Activity
- Multiple Fragments can exist in one Activity
- Has its own lifecycle, but usually mirrors the Activity lifecycle
public class SplashActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_fragment_container,
new SplashFragment(), "SplashFragment")
.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/fl_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
public class SplashFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.splash_fragment, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="40sp"
android:gravity="center"/>
</LinearLayout>
- Performs operations in the background
- Publish results in th UI thread
- Ideally for short operations
public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
finish();
}
}.execute();
}
}
- Preferred for long running operations
- Does not have a UI
- Does not necessarily have to interact with an Activity
- Uses include, and are not limited to
- Network calls
- IO
- Sending notifications
- Started Service
- Bound Service
- Intent Service
- Does one thing, and then dies
public class SplashService extends IntentService {
public SplashService() {
super(TAG);
}
public SplashService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "Hello, World!", Toast.LENGTH_LONG).show();
}
}
- Receives a broadcast
- From the system
- From the app (LocalBroadcastManager)
public class SplashBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(
context,
intent.getStringExtra("SplashServiceBroadcast"),
Toast.LENGTH_LONG
).show();
}
}
public class SplashService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Intent broadcastIntent = new Intent("SplashBroadcast");
broadcastIntent.putExtra("SplashServiceBroadcast", "Hello, World!");
sendBroadcast(broadcastIntent);
}
}
- These stuff warrant a Whirlwind Trip Part II™
- SQLite
- ContentProviders
- SharedPreferences
- Intents
- ActionBar
- Layouts and UI elements
- Widgets