Tutorial and Demos on Android in Virtual Box CSC 4320/6320 - Operating Systems Summer 2015.

Post on 23-Dec-2015

222 views 0 download

Transcript of Tutorial and Demos on Android in Virtual Box CSC 4320/6320 - Operating Systems Summer 2015.

Tutorial and Demos on Android in Virtual Box

CSC 4320/6320 - Operating SystemsSummer 2015

Outline

• Introduction of Android APP Development.• How to create an Android Virtual Machine?• How to install the Eclipse Plugin for Android?• Demo 1: Develop a Hello word App In Eclipse.• Demo 2: A simple Process Manager App.

ANDROID APP DEVELOPMENT

Introduction

• Android is an open source and Linux-based operating system for mobile devices such as smartphones and tablet computers.

• Developed by the Open Handset Alliance, led by Google, and other companies.

• Apps are developed in the Java language using the Android Software Development Kit.

• First beta version of the Android Software Development Kit (SDK) was released by Google in 2007.

• First commercial version, Android 1.0, was released in September 2008.

• Current Android version - Android 5.0 "Lollipop”.

Android vs iOS

• Pros– Easy to program : Java based– Can be developed on any OS : Mac, Windows, Linux

• Cons– Too many different types of devices to support– Quality control

Android Architecture

Android Architecture

• 4 Layered Architecture– Linux Kernel

Process management, memory management, device management– Libraries

SQLite database, web browser engine WebKit– Android Runtime

Dalvik Virtual Machine, kind of JVM optimized for Android– Application Framework

Provide high level services to applications like Telephony, Location– Applications

Android applications are written in this layer

Basic Components of Android Application

• Activities Dictate the UI and handle the user interaction to the smartphone screen • Services

Handle background processing associated with an application. • Broadcast Receivers Handle communication between Android OS and applications.

• Content Providers Handle data and database management issues.

Managing the Activity Lifecycle

Lifecycle methods

• onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy()

• Implementation of lifecycle methods is important because it ensures that application behaves well in following scenarios:– Does not crash if the user receives a phone call or switches to another

app while using your app.– Does not consume valuable system resources when the user is not

actively using it.– Does not lose the user's progress if they leave your app and return to

it at a later time.– Does not crash or lose the user's progress when the screen rotates

between landscape and portrait orientation.

Development Environment Setup

• Option 1 – Android Studio– Step 1: Set up Java SDK– Step 2: Android Studio• Option 2 – Eclipse with ADT– Step 1: Set up Java SDK– Step 2: Setup Android SDK– Step 3: Setup Eclipse IDE– Step 4: Setup Android Development Tools (ADT) Plugin

Testing the Android App Run on Emulator – Android Virtual Device

AVD Manager is used to configure a virtual device that models a specific device

Run on real Android deviceDeveloper mode should enabled on the mobile device

Run on an Android VM

HOW TO CREATE AN ANDROID VM?

How to create an Android Virtual Machine?(1)

• Download Android x-86 ISO from website– http://www.android-x86.org/download – Version: android-x86-4.4-r2.iso

• Install Android 4.4 using Virtual Box.– Follow a tutorial in http://

www.maketecheasier.com/run-android-4-3-in-virtualbox/

How to create an Android Virtual Machine?(2)

• The final screen after installing Android.

How to create an Android Virtual Machine?(4)

• Remove the installing image disk from virtual drive.

How to create an Android Virtual Machine?(3)

• Note:1.Once you are in the Welcome screen, please disable the mouse integration so that the mouse cursor will be captured in screen.– You can find the mouse integration button on the

bottom of VM window.

How to create an Android Virtual Machine?(4)

• Note:2.If your Android system is in sleep mode (black screen) , please wake up it by sending shutdown signal through VM.

HOW TO INSTALL ECLIPSE PLUG IN FOR ANDROID?

How to install Eclipse Plugin for Android?

• Install Eclipse plugin for Android Develop.• Install the latest SDK tools and platform.

Install Eclipse plugin for Android Develop(1)

• Steps– Start Eclipse, then select Help > Install New

Software.– Click Add in the top-right corner.– Enter “ADT Plugin” for the Name and following

URL for the location.https://dl-ssl.google.com/android/eclipse/

Install Eclipse plugin for Android Develop(1)

• Steps(continued)– Click OK.– In the available software dialog, select the

checkbox next to Developer Tools and click Next.– Follow the steps and Finish.– Restart Eclipse.

Install the latest SDK tools and platform

• Steps– Open Eclipse– Go to Window -> Open Perspective->Other and

Select "Java" and click OK.– Go to Window -> Android SDK Manager.

Install the latest SDK tools and platform

• Steps(continued)– Select packages “Tools”,“Android 4.4.2” and

“Extras/Android Support Library” in the package list and click “Install packages”.

How to install Virtual Machine?(2)

• Install the latest SDK tools and platform.– Select packages “Tools” and “Android 4.4.2” in the

package list and click “Install packages”.

Install the latest SDK tools and platform

• Steps(continued)– Accept the licenses for those package and click

install.

• Install the latest SDK tools and platform.– Accept the licences for those package and click

Install the latest SDK tools and platform

• Steps(continued)– Then wait for about ten to fifteen minutes.

Install the latest SDK tools and platform

• Check installed SDK in Eclipse.– Go to Eclipse -> Preferences -> Android– Check SDK installed in the Target List.(Note: if the target list is empty, please click “Browse” and select the path for folder “android-sdk” )

Install the latest SDK tools and platform

DEMO 1 : DEVELOP A HELLO WORD APP IN ECLIPSE.

Demo 1

• Configure the connection between Eclipse and Android VM.– Open VM VirtulBox Manager.– Select Android VM and click the property Network.– In Adapter 1, change network adapter attached to

“Bridge Adapter”.– Click “Advanced” and for “Promiscuous Mode”

please select “Allow All”.– Click Ok and finish the setting.

Demo 1

• Configure the connection between Eclipse and Android VM(continued).– Open Android VM.– Open Terminal Emulator.– Type netcfg– Check the IP address for eth0.– E.g. 10.250.54.183

Demo 1

• Configure the connection between Eclipse and Android VM(continued).

• For Windows OS:– Add a new system variable

• Computer -> Advanced System Setting -> Environment Variables ->New(User Variables)

• Variable Name: “ADB”• Variable Value: “;<Path of adb.exe >”

– Open command window• Type command: adb connect 10.250.54.183

Note: 10.250.54.183 is the IP address of Android VM.

Demo 1 • Configure the connection between Eclipse and Android

VM(continued).• For MacOS:

– Add a new system variable• Open a terminal• Type $open ~./profile• In the popped window, add the following text at end of text file. export PATH=$PATH:<path of adb.>export PATH=$PATH:/Users/yuanlong/Documents/MacOS_Shared/Project/P2/Eclipse_workspace/android-sdk-macosx/platform-tools

– Close terminal and reopen it• Type command: adb connect 10.250.54.183

Note: 10.250.54.183 is the IP address of Android VM.

Demo 1

• Create a simple Android app.– Eclipse -> New -> Other– Select Android Application Project, click “Next”– Enter Application Name, e.g. HelloWorldAPP– Keep the default settings for other options.– Follow the steps and finish.

Demo 1

• Important Files– Activity_main.xmlDefault file for XML layout file for the activity. Provides both text view and preview of the screen UI.– MainActivity.javaJava code for activity class that gets executed when application is run.– AndroidManifest.xmlPresents essential information about your app to the Android system like components, java package, permissions.– Strings.xmlContains all the text that your application uses.

Demo 1• srcContains the java class and application logic.– MainActivity.javaJava code for activity class that gets executed when application is run.

Demo 1• genContains automatically generated java files by ADT. Do not modify.– R.javaAn index to all the resources in “res” folder.

Demo 1• assetsStore data files. E.g. XML files.

Demo 1

• binExecutable files.– *.apkAndroid application package for install application in Android OS.(For debugging.)

Demo 1

• resContains resources, e.g. images(drawable folder), layout, strings(values folder).

Demo 1

• Code for showing process ID– Open activity_main.java– Add an id for the component TextView. <TextView android:id="@+id/textViewHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />

– Open MainActivity.java• Type the following statements at the end of onCreate().

int id= android.os.Process.myPid(); TextView text = (TextView) findViewById(R.id.textViewHello);

text.setText("Current Procee ID :"+Integer.toString(id));

Demo 1

• Code for debugging– Open MakeActivity.java– Add the following instructions at the end of

function OnCreate().Log.d("Process ID:",Integer.toString(id));

Demo 1

• Generate APK file and install it in Android VM– Right click your project -> Run Appliacation -> Run

as Android Application• Debugging– Eclipse -> Window -> Show View -> Other – Type “logcat”– Select Logcat– The print outs are stored in log info.

DEMO 2: A SIMPLE PROCESS MANAGER APP.

Demo 2

• A simple Process Manager– List the processes in Android System.– Displaying the traffic statistics.

See more at http://www.itcuties.com/android/how-to-get-running-process-list-and-traffic-statistics/#sthash.HAPRV4By.dpuf

Demo 2

• Layout design (res/layout/activity_main.xml)– Linear layout• Image • Text

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:orientation="horizontal" > <ImageView android:id="@+id/detailsIco" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appNameText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="APP NAME GOES HERE" />

</LinearLayout>

Demo 2

• Building a list for processes – Create a file ListAdapter.java under src.– Import following packages.

import java.util.List;

import android.app.ActivityManager.RunningAppProcessInfo;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;

Demo 2

• Building a list for processes(continues) – Let ListAdapter extends

ArrayAdapter<RunningAppProcessInfo>– Create a constructor for ListApdapter.

public class ListAdapter extends ArrayAdapter<RunningAppProcessInfo> { // List context private final Context context; // List values private final List<RunningAppProcessInfo> values; public ListAdapter(Context context, List<RunningAppProcessInfo> values) { super(context, R.layout.activity_main, values); this.context = context; this.values = values; }}

Demo 2

• Building a list for processes(continues) – Override method getView()

@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.activity_main, parent, false); TextView appName = (TextView) rowView.findViewById(R.id.appNameText); appName.setText(values.get(position).processName); return rowView; }

Demo 2

• Display process list and show traffic statistics– Modify src/MainActivity.java• Extends class ListActivity• Modify OnCreate() so that once app is open all the

processes are listed.• Override OnListItemClick() so that once an item in the

list is clicked the traffic statistics will be displayed.

Demo 2

• Display process list and show traffic statistics– Imported Packages

import java.util.List; import android.app.ActivityManager;import android.app.ActivityManager.RunningAppProcessInfo;import android.app.ListActivity;import android.net.TrafficStats;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.Toast;

Demo 2

• Display process list and show traffic statistics– Definition of MainActivity.java

public class MainActivity extends ListActivity {

}

Demo 2

• Display process list and show traffic statistics– Override OnCreate()

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get running processes ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses(); if (runningProcesses != null && runningProcesses.size() > 0) { // Set data to the list adapter setListAdapter(new ListAdapter(this, runningProcesses)); } else { // In case there are no processes running (not a chance :)) Toast.makeText(getApplicationContext(), "No application is running", Toast.LENGTH_LONG).show(); } }

Demo 2

• Display process list and show traffic statistics– Override OnListItemClick()

@Override protected void onListItemClick(ListView l, View v, int position, long id) { long send = 0; long recived = 0; // Get UID of the selected process int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid; // Get traffic data recived = TrafficStats.getUidRxBytes(uid); send = TrafficStats.getUidTxBytes(uid); // Display data Toast.makeText(getApplicationContext(), "UID " + uid + " details...\n send: " + send/1000 + "kB" + " \n recived: " + recived/1000 + "kB", Toast.LENGTH_LONG).show(); }

Demo 2

• Generate corresponding APK file and install it to Android VM.

Useful Resources• 1. • http://www.howtogeek.com/164570/how-to-install-android-in-virtualbox/• 2. http

://www.fixedbyvonnie.com/2014/02/install-android-4-4-kitkat-windows-using-virtualbox/

• 3. • http://www.maketecheasier.com/run-android-4-3-in-virtualbox/• 4. • http

://www.mat-d.com/site/developing-android-apps-with-android-x86-and-virtual-box/• 5.

“Android Tablet Application Development For Dummies” By Gerhard Franken• 6. The following is a link for installing Eclipse Android Development plug in.http://developer.android.com/sdk/installing/installing-adt.html#Configure

Q&A