Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Android development basics
Introduction,Structure,Development
Boncho Valkov
.Net Developer
Table of Contents
1. About Android and why to use it
2. What knowledge do you need to start
Basics of programming,OOP,Databases
3. What software (IDEs) do you need
Android Studio/Eclipse
Emulator – Genymotion or other
4. Structure of Android app
5. Main components – Intent,Activity,Service,Content
Provider,Broadcast Receiver
6. Activities lifecycle
2
Table of Contents
7. Debugging and logging
8. Intents, passing data between activities
9. Views. Layout Management
10.Listviews and Adapters
11.Notifications
12.HTTP Networking. AsyncTasks and Callbacks
13.Local Storage – SQLLite and SharedPrefs. Content Provider.
3
About Android and why to use it
4
About Android
Android is a mobile operating system (OS) based on the Linux
kernel and currently developed by Google.
Started back in 2008, today is the most popular mobile OS with
1.5 million daily activations, 1 billion devices in total (as of
April 2013).
5
About Android - Architecture
6
Why android
The most popular mobile OS
Large community
Extremely well documented APIs
Reuse existing knowledge in
Java development
Uses MVC
7
What do you have to know to start
Basics of programming in some object
oriented language like C#, Java or other
OOP principals
Databases – SQL/MYSQL or other
XML
8
What software (IDEs) do you need
1. For programming
Android studio – for a couple of months there is
official version
Eclipse plus SDK tools
2. Emulator
Genymotion
Integrated emulator in android studio
Test on actual device
9
Structure of Android project
Select API Level
Using Packages like JAVA
Java folder
Res folder
Android Manifest file
Gradle
10
Main components
Activity
Intent
Service
Content Provider
--------------------
Broadcast Receivers
Fragments
Views
11
Activity
Represents a single screen with an user
interface plus code that runs behind it
If we have multiple activities – one must be
selected as the app’s main activity.
The main activity is similar to the main()
function in Java and C#
12
Intent
Objects carrying messages from one
component to another (within the
application or outside it).
Two types of intents:
•
Explicit intents – tell exactly what do
you want to start to get some result
•
Implicit intents – tell that you what
some result, without specifying how
13
Service
Runs in the background to perform long- running operations.
Doesn’t need to interact with the user.
14
Content Providers
Supplies data from one application to
others on request.
- Can use different ways to store data
(database, files, over a network).
15
Broadcast Receivers
Respond to broadcast messages from other applications or from
the system itself (called events or intents).
Applications can also initiate broadcasts to let other
applications know that some data has been downloaded or
processed.
16
Fragments
Kind of sub-activity
Can be placed in an Activity to achieve more modular design.
-You can add or remove fragments in an activity while the
activity is running.
17
Views
Layouts
Widgets
Menu
Drawer
Tabs
Can be created via:
-XML files
Programmatically
18
Activity Lifecycle
Activity Lifecycle
onCreate
onStart
onResume – application is running
onPause
onStop
onDestroy
20
Demo
Debugging and logging
Debugging and logging
F8 – step forward
F9 – go next breakpoint
Log.v() -VERBOSE
Log.d() -DEBUG
Log.i() -INFO
Log.w() -WARN
Log.e() -ERROR
23
Demo
Intents, passing data between
activities
Intents
What is an intent
messages you can pass between your app components
can also send them to components in other apps,
and execute a task
like playing music,
sending email
and taking pictures.
26
Intents
With an Intent you can
Start activities
Start services
Deliver broadcasts
Pass data
27
Intents
Types of intents
Implicit
have not specified a component
they must include enough information for the system to determine
which of the available components is best to run for that intent.
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://www.softuni.bg "));
startActivity(intent);
28
Intents
Implicit
29
Intents
Types of intents
Explicit
have specified a component, which provides the exact class to be
run
usually is used for starting internal activities
Intent intent = new Intent (this,SecondActivity.class);
startActivity(intent);
30
Passing data between activities
Put the data in the intent
Intent intent = new Intent (this,SecondActivity.class);
intent.putExtra("data","i have been passed");
startActivity(intent);
Retrieve the data from the intent from the component that has
been started, usually in onCreate method
Intent intent = getIntent();
String passedData = intent.getExtra(“data”);
Serializable objects can be passed
31
Intent Filters
We use the intent filters to deny or allow intents to reach our
app’s components.
<action android:name="android.intent.action.SEND"/>
<data
android:mimeType="text/plain"/>
<category android:name="android.intent.category.DEFAULT"/>
Restrict access
<activity android:exported = false>
32
Demo
Views. Layout Management
Views. Layout Management
Mandatory view props
android:layout_width
android:layout_height
Match parent
Fill parent
Wrap Content
35
Linear Layout
Single direction, vertically or horizontally
Specified by the android:orientation attribute
All children are stacked one after another
Can use gravity
36
Relative Layout
Displays child views in relative positions
The position of each view can be specified as relative to sibling
elements
Can align two elements by right border
37
Demo
List Views and Adapters
List Views and Adapters
Parts of the ListView:
Rows
Adapter -bridge between a
view and the underlying data
for that view. Provides access
to the data items.
Fast Scrolling
Section Index
40
List Views and Adapters
Types of list views and adapters
41
Simple List View
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Create ArrayAdapter
Set Adapter to the listView
Set OnClickListener
42
Custom List View Adapter
Most of the time we need something
specific
Create custom row
Create Custom Adapter that extents
BaseAdapter
Implement required methods
convertView = LayoutInflater.from(context)
.inflate(R.layout.university_row,parent,false);
43
Demo
Notifications
Notifications Components
Three required components
Icon
Content title
Content text
Optional components
Action attached to notification, usually an Activity, it’s done by
Pending Intent, by calling set Content Intent
46
Notification sample
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
If want to add action to Notification
Create Intent
Create PendingIntent, by PendingIntent.getActivity
Use setContentIntent to attach the pending Intent
47
Demo
Along with the live demo
HTTP Networking. AsyncTasks
and Callbacks
Http
Hypertext Transfer Protocol
(HTTP)
HTTP is the foundation of data
communication for the World
Wide Web.
Needs permmitions
HttpClient defaultHttpClient = new DefaultHttpClient(new
BasicHttpParams());
HttpPost httpPost = new HttpPost(path);
HttpResponse response = defaultHttpClient.execute(httpPost);
50
AsyncTask
Class that that is being executed on a different thread
Prevent using the Main thread (UI) for time taking operations
Extend class with AsyncTask<String,Void,String>
51
Demo
Local Storage – SQLLite and
SharedPrefs. Content Provider.
Content Providers
Manage access to a structured set of data
Encapsulate the data, and provide mechanisms for defining data
security
The standard interface that connects data in one process with
code running in another process.
54
SQLite
Software library that implements a self-contained, zeroconfiguration, transactional SQL database engine.
The most widely deployed SQL database engine in the world.
Useful tools and queries:
- DB Browser for SQLite
Create, Select,
Delete, Update
55
Local storage
Shared Preferences Store private primitive data in key-value
pairs.
SharedPreferences sp = getSharedPreferences(PREFS,
Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("key", "value");
editor.commit();
if (sp.contains(Name))
name.setText(sp.getString(Name, ""));
56
Demo
Questions?