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
Programming for Android Boutsis Ioannis [email protected] History ● ● ● Android is developed by the Open Handset Alliance led by Google Google purchased the initial developer of the software, Android Inc., in 2005 Android's source code is released by Google under open source licenses Android Growth ● ● ● As of July 2013, the Google Play store had over one million Android apps published, and over 50 billion downloads At Google I/O 2014, the company revealed that there were over one billion active monthly Android users, up from 538 million in June 2013 A 2015 survey found that 40% of full-time professional developers see Android as their priority target platform Statistics Platform Characteristics ● ● ● This is a mobile OS with limited resources – Limited memory, processing power – Battery life is critical! This results in specific architectural decisions – The OS aggressively flushes memory when not in use – (i.e. it will page apps in and out of memory) – Background computation is extremely limited Unusual device characteristics – Small screen, typically focused on a single app – Gestural input Architectural Overview ● ● Layered environment – Linux 2.6 kernel – Core Android libraries/services built on top – Top‐most layer runs in a VM Application management – Applications are built in Java, running in a Dalvik VM (now ART) – Each application is a separate user/process/VM! – Applications can share services and capabilities Architecture ● Applications All applications are written using the Java programming language. ● Application Framework By providing an open development platform, Android offers developers the ability to build extremely rich and innovative applications. Developers are free to take advantage of the device hardware, access location information, run background services, set alarms, add notifications to the status bar, and much, much more. Developers have full access to the same framework APIs used by the core applications. ● Libraries Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. ● Linux Kernel Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack. Architecture Components ● ● ● ● ● ● Core building blocks for Android applications Activities: Single‐screen of an application / different activity for each UI screen of the application. Services: Long‐running background application (e.g. playing music on the background) / can be bind with a specific activity. Content Providers: Used to manage access on persistent data that can be shared on different applications (e.g. contacts phone numbers). Broadcast Receivers are used to receive a notification and respond to that (e.g. receiving an SMS).. Components communicate with messages called Intents, which are routed through Android runtime and the Kernel. Activity Lifecycle ● Your application probably consists of one or two main activities, each containing a set of views (i.e. Java views and components) Activity Lifecycle II ● We focus on activities (i.e. typical UI screens) – Apps can contain multiple activities (one “main”) – Activities can create other activities (“Back stack”) – Navigation forward/back triggered by user actions How to install Android SDK Required Installations Java JDK http://www.oracle.com/us/technologies/java/index.html Android Studio or Eclipse(NetBeans etc..) http://www.eclipse.org/downloads/ Download Android Studio http://developer.android.com/sdk/index.html Installing Started Package ● In Windows – Easy, guide.. ● In Linux – Extract to specific folder ● Android SDK will be installed at startup Setting up your SDK ● Tools → Android → SDK Manager INSTALL Android Versions Create new Android Virtual Device ● Tools → Android → AVD Manager Run Project as Android application ● When executing an android project through Android Studio we should now be able to choose our mobile/simulator Ready to Start!! Programming... ● Android Manifest ● XML based User Interface ● Java Code http://developer.android.com/guide/topics/ui/index.html Android Manifest ● ● ● ● Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system. Activities, Permissions, Minimum Android Level, etc. should be declared in the manifest. More Info: http://developer.android.com/guide/topics/manifest/manifest-intro.html AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".TestAndroidActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity2" </activity> </application> </manifest> > Resources(/res) ● /drawables (low, medium, high) ● /layout (xml Uis) ● /values eg string.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="ds2015">Distributed Systems</string> <string name="button_enter">Enter</string> <string name="click_android">Click on the android</string> <string name="changed_text">Text to be changed</string> <string name="app_name">TestAndroid</string> </resources> Layouts main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/ds2015" android:textSize="20sp" android:textStyle="bold" android:gravity="center_vertical|center_horizontal"/> <ImageView android:id="@+id/androids" android:layout_width="234dp" android:layout_height="246dp" android:src="@drawable/android1" /> <Button android:id="@+id/button1" android:layout_width="150dp" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="@string/button_enter" /> </LinearLayout> Layouts activity2.xml <?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" android:background="#330000" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/talking_android" android:layout_width="180sp" android:layout_height="80sp" android:layout_marginBottom="50sp" android:src="@drawable/android_look" /> <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:textSize="16sp" android:textStyle="italic" android:text="@string/click_android" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:background="#000066" android:textColor="#ffcc00" android:typeface="monospace" android:text="@string/changed_text" /> </LinearLayout> Java Code TestAndroidActivity.java package com.test.android; import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Button; public class TestAndroidActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button next = (Button) findViewById(R.id.button1); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent myIntent = new Intent(view.getContext(), Activity2.class); startActivityForResult(myIntent, 0); } }); } } Java Code Activity2.java private void androidClick() { final AlertDialog.Builder alert = new AlertDialog.Builder(this); final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("Change text", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString().trim(); changeText(value); } }); package com.test.android; import import import import import import import import import android.app.Activity; android.app.AlertDialog; android.content.DialogInterface; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.ImageView; android.widget.TextView; android.widget.Toast; public class Activity2 extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }); final ImageView iv = (ImageView)findViewById(R.id.talking_android); alert.show(); // set the listener iv.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { androidClick(); } }); } private void changeText(String value) { TextView tv = (TextView) findViewById(R.id.textView1); tv.setText(value); Toast.makeText(getApplicationContext(), "Text changed!", Toast.LENGTH_SHORT).show(); } } } Java Code Διευθυνσιοδότηση ● ● ● ● Στα κινητά οι διευθύνσεις ip δίνονται από το δίκτυο. Για όσους χρησιμοποιούν τον emulator οι διευθύνσεις ip που χρησιμοποιεί είναι διαφορετικές από αυτές του υπολογιστή http://developer.android.com/guide/developing/devices/emulator.html#emulatornet working Έτσι, για να συνδεθεί ένας client στο ServerSocket του emulator πρέπει να γίνουν τα εξής: – -το serverSocket του emulator ανοίγει στο port που θέλουμε(έστω 5000) – -Στη συνέχεια πρέπει να κάνουμε redirect κάποιο port σε αυτό. – Ανοίγουμε ένα telnet στο port που τρέχει ο emulator(πχ 5554) με telnet localhost 5554 – και εκτελούμε redir add tcp:5001:5000 – -Τώρα κάθε client μπορεί να δημιουργήσει ένα socket για να συνδεθεί στο αρχικό serverSocket με IP/port: 10.0.2.2:5001 http://developer.android.com/guide/developing/devices/emulator.html#connecting