android 101 session @thejunction32

44
Android 101 ההההה הההההה: הה הה ?Android ההה ההה הההההה? ההה הההה הההההה הה ההההה ?Android הההה הההה הה ההההה?Debugging

Upload: thejunction32

Post on 21-Jun-2015

582 views

Category:

Technology


0 download

DESCRIPTION

Android engineering 101,

TRANSCRIPT

Page 1: Android 101 Session @thejunction32

Android 101

ההרצאה :נושאי

זה ?• Androidמה

•? העבודה כלי מהם

יישום ? • של הבניין אבני Androidמהם

• ? בשביל יש כלים Debuggingאיזה

Page 2: Android 101 Session @thejunction32

What’s Android?

• An open source mobile operating system.

• Linux kernel base.

• Inc. open source.

• Custom Application

Framework layer.

Page 3: Android 101 Session @thejunction32

What’s given by Android for development?• ADB.• AVD manager and emulator.• Android devices.• SDK.• NDK.

Android tools.

Page 4: Android 101 Session @thejunction32

The “Android Debug Bridge” is: • A command line tool.• handles communication with a development unit• a client-server program that includes three

components:1. A client, which runs on your computer. 2. A server, which runs as a background process.3. A daemon, which runs as a background process on each

development unit.

Android tools – ADB.

Page 5: Android 101 Session @thejunction32

• Runs on your computer.• Mouse/keyboard to mimic touch and key

events.• Mimics all hardware and software features.– Different screen sizes and densities.– Emulating phone events (SMS, phone calls).– Spoofing location events.– Different API levels.

Android tools – Emulator.

Page 6: Android 101 Session @thejunction32

Android tools – Emulator.

Page 7: Android 101 Session @thejunction32

• All android devices are debug-able via USB “out of the box” (no root is needed).

• To enable:– Settings>applications:

• Allow unknown resources.

– Settings>applications>development:• Allow USB debugging.• Stay awake.

Android tools – Device.

Page 8: Android 101 Session @thejunction32

• Programming language: JAVA + XML.• Create standard android application by using

and utilizing the applications framework.• Most popular IDE:– Eclipse + ADT.

• Other possible IDEs:– intellij.– Net beans.

Android tools – SDK.

Page 9: Android 101 Session @thejunction32

• Programming language: everything!• Create an android application by accessing the

native code directly.• Reasons for using NDK:– Using non-java ready made code.– Using hardware acceleration.– A real need for more performance.

Android tools – NDK.

Page 10: Android 101 Session @thejunction32

Android building blocksThe components of an android

application and their connecting methods.

Page 11: Android 101 Session @thejunction32

The (Android) application.

An application consists of• A manifest xml.• A set of:–Activities.–Services.–Broadcast receivers.–Content providers.

Page 12: Android 101 Session @thejunction32

The Manifest

The Manifest is essentially a list of components that the application uses or requires, such as:• The components of the applications.• User permissions .• The minimum API level.• Hardware and software features.• API dependencies.

Page 13: Android 101 Session @thejunction32

A manifest example

Page 14: Android 101 Session @thejunction32

.The Activity

• An activity represents a single screen with a user interface (Java file + xml layout file).

• An application usually consists of multiple activities that are loosely bound to each other.

• One Activity is flagged as “main” and it is started at the application launch time.

• An Activity can launch other activities to create an app UI workflow.

• The Activity has an Intent attribute that determines how android treats it.

Page 15: Android 101 Session @thejunction32

A screen that displays a list of emails.

A screen that displays a single email.

A screen that enables you to compose an email.

Each and every one of these is an Activity!

Activity explained with an example.

Page 16: Android 101 Session @thejunction32

Starting an Activity.

When you press reply:

• The compose Activity gets called.• The recipients are sent via the“Extras” attribute of the intent.

Page 17: Android 101 Session @thejunction32

The Activity life-cycle.

• There are 6 stages in the Android Activity life cycle.

• These stages allow you to perform tasks in their correct time of the workflow.

Page 18: Android 101 Session @thejunction32

The Activity life-cycle vol2.

Page 19: Android 101 Session @thejunction32

SharedPrefs – A tool that saves data to a file so it will be available even if the app is closed and restarted.It is generally used for:1. Saving user preferences.2. Saving a “snapshot” of the app so it could be resumed to precisely the same state.

The Activity life-cycle vol3 – before example.

Page 20: Android 101 Session @thejunction32

The Activity life-cycle vol3 – example of use.

Page 21: Android 101 Session @thejunction32

• Consists of xml.• Describe the static layout of a screen.A few types available:– LinearLayout – children are lined up automatically.

– RelativeLayout - children order needs to be defined.

– ListView – children are items of a list.– Etc.

The Activity – Layout.

Page 22: Android 101 Session @thejunction32

The Activity – Layout example.

Page 23: Android 101 Session @thejunction32

.The Service

An application component that can perform long-running operations in the background and does not provide a user interface.

The service has two forms:• A "started“ service is called by an app component and runs in

the background even if the application that called it was closed.

• A “bound” service is bound by an app component and is used as a client-server bridge, this sort of service is terminated when the application that is bound to it is closed.

Page 24: Android 101 Session @thejunction32

Starting a service.

From the activity perform:

The startService() method returns immediately.If the service is not already running, the android system calls onCreate(), then calls onStartCommand() else it only calls the onStartCommand() method.

Page 25: Android 101 Session @thejunction32

The same principle as in the activity life cycle applies here only the stages are different.

As you can see the bound and started services have a similar cycle with a twist:

The bound service is only “alive” while the binding application is “alive”.

The service life cycle.

Page 26: Android 101 Session @thejunction32

The service life cycle.

Page 27: Android 101 Session @thejunction32

• a component that responds to system-wide broadcast announcements.

• The announcement can originate either from a system event (screen on/off) or a custom activity event.

• When this protocol is customized it facilitates message sending between applications and activities that should work together.

The Broadcast receivers.

Page 28: Android 101 Session @thejunction32

The manifest tag:

The Broadcast receivers - example.

Page 29: Android 101 Session @thejunction32

The broadcast receiver file:

The Broadcast receivers - example.

Page 30: Android 101 Session @thejunction32

• Components that manage a shared set of application data.

• The data is stored at one of the following formats:1. In the file system.2. In an SQLite database.3. on the web.4. More..

Essentially you can store it in any persistent storage location your application can access.

The Content providers.

Page 31: Android 101 Session @thejunction32

Through the content provider, other applications can query or even modify the data (if the content provider allows it).

providers are also useful for reading and writing data that is private to your application and is not shared.

The Content providers.

Page 32: Android 101 Session @thejunction32

Here’s a few examples for system content providers:• Contacts.• Text messages.• Phone calls.

The Content providers – examples.

Page 33: Android 101 Session @thejunction32

• Android provides a few precious tools for that:– DDMS.– Logcat.– Hierarchy viewer.– Monkey & monkey runner.

Android debug tools.

Page 34: Android 101 Session @thejunction32

Android debug tools – DDMS.

The “Dalvik Debug Monitor Server (DDMS)” is: • A debugging tool that provides:1. Port forwarding services. 2. Screen capture.3. thread and heap information.4. Logcat and process logging.5. Mobile data spoofing (location, calls and SMS)6. Memory dump.

Page 35: Android 101 Session @thejunction32

Android debug tools – DDMS.

Page 36: Android 101 Session @thejunction32

• A mechanism for collecting and viewing system debug output.

• Filter levels:– V — Verbose (lowest priority)– D — Debug– I — Info– W — Warning– E — Error

• Invoke logs with a custom TAG in your app to monitor its operation:

Android debug tools – Logcat.

Page 37: Android 101 Session @thejunction32

• Displays the View objects that form the UI of the Activity that is running on your device or emulator.

• Start command: <sdk>/tools/hierarchyviewer• If #levels > 7 -> NOT GOOD!

Android debug tools – Hierarchy viewer.

Page 38: Android 101 Session @thejunction32

• Creates random UI events.• Great UI durability test.• You can configure:– The amount of event.– Percentage of touch/motion/track/key events.– The monkey’s “aggressiveness” (die after

exception/error/security error etc.).

Android debug tools – Monkey.

Page 39: Android 101 Session @thejunction32

• Enables using the monkey tool in a more precise way:– Write a python code.– The monkey performs it.

• Great for recreating bugs and create tests to see when they are resolved.

Android debug tools – Monkey runner.

Page 40: Android 101 Session @thejunction32

Tips and pointers - ANR.

• “Activity not responding”:– Appears if:• 5 seconds of no action after a

touch event.• 10 second after invoking a BR

it hasn’t finished working.

– Why?• Every activity has 1 UI thread

and if it is stuck the UX suffers.

Page 41: Android 101 Session @thejunction32

• Everything that isn’t a UI action shouldn’t be on the UI thread, use:– AsyncTask.– TimerTask.– Handler.– Scheduled task.– Thread.Then post your data on the UI thread.

Tips and pointers – Avoid ANR.

Page 42: Android 101 Session @thejunction32

Create an AsyncTask:

start an AsyncTask:

Tips and pointers – example.

Page 43: Android 101 Session @thejunction32

• Many different devices = Many different screen sizes.– Prepare by:• Use wrap_content, fill_parent, or dp units for layouts

(no PX reframe from hardcoded values altogether).• Test on many different Emulator instances.• Use 9patch images (they stretch correctly). • Add per-resolution drawable resource folder:

• - layout-small/…• - layout-xlarge/..

Tips and pointers – Multiple screens.

Page 44: Android 101 Session @thejunction32

3 main possibilities:1. GPS.

1. Most accurate.

2. Wireless networks.1. Gives faster results.2. Costs less battery.3. Works inside buildings.

3. Cell towers.1. Always available.

Tips and pointers – location detection.