Developer Instruments for Android Android ArtfulBits Inc. Oleksandr Kucherenko.
Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer.
-
Upload
chad-atkins -
Category
Documents
-
view
223 -
download
2
Transcript of Android development basics Introduction,Structure,Development Boncho Valkov.Net Developer.
Android development basicsIntroduction,Structure,Development
Boncho Valkov.Net Developer
Table of Contents
1. About Android and why to use it2. 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 app5. Main components – Intent,Activity,Service,Content
Provider,Broadcast Receiver6. Activities lifecycle 2
Table of Contents
7. Debugging and logging8. Intents, passing data between activities9. Views. Layout Management10.Listviews and Adapters11.Notifications12.HTTP Networking. AsyncTasks and Callbacks13.Local Storage – SQLLite and SharedPrefs. Content Provider.
3
4
About Android and why to use it
5
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).
About Android
6
About Android - Architecture
7
The most popular mobile OS Large community Extremely well documented APIs Reuse existing knowledge in Java development Uses MVC
Why android
8
Basics of programming in some object oriented language like C#, Java or other
OOP principals Databases – SQL/MYSQL or other XML
What do you have to know to start
9
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
What software (IDEs) do you need
10
Select API Level Using Packages like JAVA Java folder Res folder Android Manifest file Gradle
Structure of Android project
11
Main components
Activity Intent Service Content Provider -------------------- Broadcast Receivers Fragments Views
12
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#
Activity
13
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
Intent
14
Runs in the background to perform long- running operations. Doesn’t need to interact with the user.
Service
15
Supplies data from one application to others on request.
- Can use different ways to store data (database, files, over a network).
Content Providers
16
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.
Broadcast Receivers
17
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.
Fragments
18
Layouts Widgets
Menu Drawer Tabs
Can be created via: -XML files Programmatically
Views
Activity Lifecycle
20
onCreate onStart onResume – application is running onPause onStop onDestroy
Activity Lifecycle
Demo
Debugging and logging
23
F8 – step forward F9 – go next breakpoint Log.v() -VERBOSE Log.d() -DEBUG Log.i() -INFO Log.w() -WARN Log.e() -ERROR
Debugging and logging
Demo
Intents, passing data between activities
26
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.
27
With an Intent you can Start activities Start services Deliver broadcasts Pass data
Intents
28
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.
Intents
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://www.softuni.bg ")); startActivity(intent);
29
Intents
Implicit
30
Types of intents Explicit
have specified a component, which provides the exact class to be run
usually is used for starting internal activities
Intents
Intent intent = new Intent (this,SecondActivity.class); startActivity(intent);
31
Put the data in the intent
Retrieve the data from the intent from the component that has been started, usually in onCreate method
Serializable objects can be passed
Passing data between activities
Intent intent = getIntent();String passedData = intent.getExtra(“data”);
Intent intent = new Intent (this,SecondActivity.class);intent.putExtra("data","i have been passed"); startActivity(intent);
32
We use the intent filters to deny or allow intents to reach our app’s components.
Restrict access
Intent Filters
<activity android:exported = false>
<action android:name="android.intent.action.SEND"/> <data android:mimeType="text/plain"/> <category android:name="android.intent.category.DEFAULT"/>
Demo
Views. Layout Management
35
Views. Layout Management Mandatory view props
android:layout_width android:layout_height
Match parent Fill parent Wrap Content
36
Single direction, vertically or horizontally Specified by the android:orientation attribute All children are stacked one after another Can use gravity
Linear Layout
37
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
Relative Layout
Demo
List Views and Adapters
40
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
List Views and Adapters
41
List Views and Adapters
Types of list views and adapters
42
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
43
Most of the time we need something specific
Create custom row Create Custom Adapter that extents
BaseAdapter Implement required methods
Custom List View Adapter
convertView = LayoutInflater.from(context)
.inflate(R.layout.university_row,parent,false);
Demo
Notifications
46
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
Notifications Components
47
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
DemoAlong with the live demo
HTTP Networking. AsyncTasks and Callbacks
50
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);
51
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>
AsyncTask
Demo
Local Storage – SQLLite and SharedPrefs. Content Provider.
54
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.
Content Providers
55
Software library that implements a self-contained, zero-configuration, 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
SQLite
56
Shared Preferences Store private primitive data in key-value pairs.
Local storage
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, ""));
Demo
Questions?