Differences

This shows you the differences between two versions of the page.

Link to this comparison view

smd:laboratoare:01 [2018/02/26 17:22]
vlad.traista
smd:laboratoare:01 [2021/03/17 19:58] (current)
adriana.draghici
Line 1: Line 1:
-===== Laboratorul ​01. ===== +===== Lab 01. Introduction ​=====
  
 === Resources === === Resources ===
  
-  * Android Studio: ​[[http://​developer.android.com/sdk/index.html#top]] +  * [[https://​developer.android.com/​index.html|Android Developers Documentation]] 
-  * Android Developers: ​[[https://​developer.android.com/​index.html]] +  * [[https://​developer.android.com/​guide/​components/​activities/​activity-lifecycle|Android Application Lifecycle]]
- +
-=== Files ===+
  
-{{:ndk:courses:lab-1.zip|}}+**Prerequisites:** [[smd:laboratoare:00|Lab 0: Setup github and your environment]]
  
-=== Task 0 Setting up the environment ===+The code skeleton used from task#3:  
 +  * [[https://​github.com/​SMD-UPB/labs| Github repository]] 
 +  * {{:​smd:​laboratoare:​loglevel.zip|}}
  
-To install Android Studio on your own machine follow the step here: [[http://​developer.android.com/​sdk/​installing/​index.html?​pkg=studio|Installing Android Studio]]. Android studio is already installed on the workstations in the lab. 
  
 === Task 1 - Create and run an application === === Task 1 - Create and run an application ===
  
-Create a new Android Project in Android Studio (**Start a new Android Studio Project**). ​Choose a name and leave other options as they are. On the next screen ​choose ​**Phone and Tablet** ​and choose ​API level 15 or higher. Choose ​**Empty Activity** ​on the next screen. Continue to the next screen and click finish. On the sidebar on the left choose ​project ​and open ** java > student.example.com > MainActivity**. This activity is very simple right now.+Create a new Android Project in Android Studio (**Start a new Android Studio Project**). ​In the first screen ​in the **Phone and Tablet** ​section, ​choose **Empty Activity**. On the sidebar on the left choose ​**Project** ​and open ** java > student.example.com > MainActivity**. This activity is very simple right now but is a fully functional Android application.
  
-Run the project by clicking the green arrow on the toolbar above the editing area. Android Studio will prompt you to choose a device. Follow the steps in the next tasks to create a new virtual device.+Run the project by clicking the green arrow on the toolbar above the editing area, {{:​smd:​laboratoare:​run.png?​20|}}. Android Studio will prompt you to choose a device. Follow the steps in the next task to create a new virtual device.
  
-=== Task 2 - Creating a virtual device ===+/*=== Task 2 - Creating a virtual device ===
  
 Create a virtual device: Create a virtual device:
-  - Click **...** next to the **Android Virtual Device** list under the **Launch emulator** option +  - Click the green arrow (Run) to run the program, {{:​smd:​laboratoare:​run.png?​20|}} 
-  - Click **Create Virtual Device**. +  - In the menu click **Create ​New Virtual Device**. 
-  - Choose Nexus or Galaxy Nexus screen resolution. Click Next.+  - Choose Nexus 5X or Pixel 2 XL screen resolution. Click Next.
   - Choose Oreo x86 image (or any other image above API 24). Download if needed. Click next.   - Choose Oreo x86 image (or any other image above API 24). Download if needed. Click next.
-  - Give the device 512-1024 MBs of RAM and 64 MB VM Heap (Under ​Advance Options)+  - Give the device 512-1536 MBs of RAM and 64-226 MB VM Heap (Under ​Show Advanced Settings)
   - Click **Finish**   - Click **Finish**
-  - Select the Android Virtual Device that you just created and press **OK**. This will launch the emulator. Wait for it to boot and check that your empty application starts.+  - Select the Android Virtual Device that you just created and press **OK**. This will launch the Emulator. Wait for it to boot and check that your empty application starts.
  
-In order for the Android Virtual Device to benefit from the hardware acceleration support of your processor, depending on the operating system, you will need to have installed/​enabled the following:+In order for the Android Virtual Device to benefit from the hardware acceleration support of your processor, depending on the operating system, you will need to have installed/​enabled ​one of the following:
   - [[https://​developer.android.com/​studio/​run/​emulator-acceleration.html#​vm-windows|HAXM]] on Windows   - [[https://​developer.android.com/​studio/​run/​emulator-acceleration.html#​vm-windows|HAXM]] on Windows
   - [[https://​developer.android.com/​studio/​run/​emulator-acceleration.html#​vm-mac|HAXM]] on MAC   - [[https://​developer.android.com/​studio/​run/​emulator-acceleration.html#​vm-mac|HAXM]] on MAC
Line 41: Line 39:
 ./​Android/​Sdk/​tools/​emulator -accel-check ./​Android/​Sdk/​tools/​emulator -accel-check
 </​code>​ </​code>​
 +*/
  
-=== Task - Add a button ===+=== Task - Add a button ===
  
-The MainActivity.java file should be displayed. If not, open it using the **Package Explorer** pane. You will find it in the **src** folder.+When the project has opened the //MainActivity.java// file should be displayed. If not, open it using the **Package Explorer** pane. You will find it in the **src** folder.
  
-The Activity should be very simple. It has an onCreate method which gets called when the Activity gets started.+The Activity should be very simple. It has an **onCreate** method which is called when the Activity gets started.
  
-In the onCreate method ​the Activity calls+The ''​onCreate'' ​method:
 <​code>​ <​code>​
 setContentView(R.layout.activity_main);​ setContentView(R.layout.activity_main);​
 </​code>​ </​code>​
-This sets the layout for the Activity. **R** is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the **res** folder, in this case **layout**.+This sets the layout for the Activity. ​
  
-Open the //​res/​layout/​activity_main.xml//​ file. You will see the layout ​editor. In the **Palette** pane, find the button ​and drag it to the activity. You will see the **Attributes** panel to the right of the screen change to reflect this. Select the button and check its ID field in the **Attributes** panel. Change the text of the button to "Press me!". Also check the ID field of the default TextView element.+<note tip>​**R** is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the **res** folder, in this case **layout**. 
 + 
 +</​note>​Open the //​res/​layout/​activity_main.xml//​ file. You will see the **Layout ​editor**. In the **Palette** pane, find **Button** ​and drag it to the layout. Set the constraints for the button. You will see the **Attributes** panel to the right of the screen change to reflect this. Select the button and check its //ID// field in the **Attributes** panel. Change the text of the button to "Press me!". Also check the //ID// field of the default ​**TextView** element.
  
 Go back to MainActivity.java. Go back to MainActivity.java.
  
-Add the following code to the onCreate method. Make sure the ids match the ones in your layout.+Add the following code to the ''​onCreate'' ​method. Make sure the //​IDs// ​match the ones in your layout.
 <code Java> <code Java>
-        ​final Button ​b1 (Button) ​findViewById(R.id.button);​ /* make sure the button ID matches */ +        Button ​button ​= findViewById(R.id.button);​ /* make sure the button ID matches */ 
-        final TextView ​tv1 (TextView) ​findViewById(R.id.textView);​ /* make sure the textView ID matches */+        final TextView ​textView ​= findViewById(R.id.textView);​ /* make sure the textView ID matches */
         ​         ​
-        ​b1.setOnClickListener(new OnClickListener() {+        ​button.setOnClickListener(new ​View.OnClickListener() {
         int n = 1;         int n = 1;
                 
  @Override  @Override
  public void onClick(View v) {  public void onClick(View v) {
- tv1.setText("​Hello again! " + Integer.toString(n++));​+ textView.setText("​Hello again! " + Integer.toString(n++));​
  }  }
  });  });
 </​code>​ </​code>​
  
-The **findViewById** calls are used to get the objects corresponding to the Button and the TextView that were already declared. ​These objects are declared as final variables ​in order for them to be visible inside the **OnClickListener** call (anonymous class). The **OnClickListener** object contains an onClick method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button.+The **findViewById** calls are used to get the objects corresponding to the Button and the TextView that were already declared. ​TextView is declared as //final// in order to be visible inside the **View.OnClickListener** call (anonymous class). The **View.OnClickListener** object contains an **onClick** method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button.
  
-=== Task 4 - Use logcat for debugging ===+Run the project again to see the changes.
  
-Start the application installed in Task 2. Select the **Logcat** tab (situated by default in the bottom panel of Android Studio). This panel shows all the log entries written by the Android applications running on connected mobile devices or emulators. It is possible to filter these log entries: above the logs themselves there is a text entry input which allows textual filtering. It allows you to limit scope by prefixing searches with pid:, tag:, app: or text: and it accepts Java-style regular expressions. To the left of this text field there is a drop-down which allows you to select verbosity. Choosing a level will only allow messages of that level or higher to be displayed.+=== Task Use logcat for debugging ===
  
-Import ​the LogLevel project into Android Studio (**File > New > Import Project** and navigate to the project ​in the archiveLook over the code. Each button has a method attached which calls a static method from the Log class. These methods generate ​log entries on different levels of importance: d=debug, i=info, w=warning, e=error.+Select ​the **Logcat** tab (situated by default ​in the bottom panel of Android Studio)This panel shows all the log entries ​written by the Android applications running ​on connected mobile devices or emulators
  
-Add another two buttons ​to the applicationone that generates an assert level log entry and one that repeats ​the previously generated log entry, but with a higher level of importance (e.gpreviously ​debug entry was created, now the button click should generate an info entry)For the second button if the previous entry was an assert entry, then the next entry will also be an assert entryIf there is no previously generated log entrythen a debug entry will be logged.+It is possible ​to filter these log entriesabove the logs themselves there is a text entry input which allows textual filteringIt accepts //​Java-style regular expressions//​To the left of this text field there is drop-down which allows you to select //​verbosity//​Choosing a level will only allow messages of that level or higher to be displayedTo the right of the text field there is a drop-down which allows you to create custom filters. Click on it and select //Edit Filter Configuration//​. From here you can create different filters which limit searches scope using //PID:////Tag://, //Package Name:// or //​Message://​.
  
-=== Task 5 - View weight ===+**Import the LogLevel project** into Android Studio (**File > New > Import Project**) and navigate to the project in the archive. Look over the code. Each button has a method attached which calls a static method from the //Log// class. These methods generate log entries on different levels of importance: //d=debug//, //i=info//, //w=warning//, //e=error//.
  
-Use the LogLevel project as a start. Notice ​that the EditText box only takes up one line of the screen and there should be some screen space left at the bottom ​of the screenMake the EditText box take up all available spaceTo do thisyou should ​set the weight parameter of the EditText view to 1 (or any integer).+Add another two buttons to the application:​  
 +  * one that generates an assert level log entry  
 +  * and one that repeats ​the previously generated log entry, but with a higher level of importance (e.gpreviously a //debug// entry was creatednow the button click should ​generate an //info// entry). 
  
-The weight parameter is used to distribute empty screen space to the elements which need it. First, the layout builder will place all the elements for which the size is easy to determine. Then, the empty space will be distributed to the elements with weight, based on their weight: if the total weight of all elements is 4, an element with a weight of 1 will get 1/4 of the empty screen space, an element with 2 weight will get 2/4, and so on.+For the second button if the previous entry was an //assert// entry, then the next entry will also be an //assert// entry. If there is no previously generated log entry, then a //debug// entry will be logged. 
 + 
 +=== Task 4 - View weight === 
 + 
 +Use the LogLevel project as a start.  
 + 
 +Notice that the **EditText** box only takes up one line of the screen and there should be some screen space left at the bottom of the screen.  
 +  * Make the EditText box take up all available space.  
 +  * To do this, you should set the **weight** attribute of the EditText view to **1** (or any integer). 
 + 
 +The **weight** parameter is used to distribute empty screen space to the elements which need it. First, the layout builder will place all the elements for which the size is easy to determine. Then, the empty space will be distributed to the elements with //weight//, based on their //weight//: if the total //weight// of all elements is 4, an element with a //weight// of 1 will get 1/4 of the empty screen space, an element with 2 //weight// will get 2/4, and so on. 
 + 
 +=== Task 5 - Understand Activity Lifecycle ===  
 + 
 +[[https://​developer.android.com/​guide/​components/​activities/​activity-lifecycle|Activity Lifecycle]] 
 + 
 +The Activity goes through a series of different states in its **lifecycle**. **onCreate**,​ **onStart**,​ **onResume**,​ **onPause**,​ **onStop**, **onRestart** and **onDestroy** are the callbacks from the [[https://​developer.android.com/​reference/​android/​app/​Activity|Activity]] class which inform the activity about transitions through states:  
 +  * creating the activity 
 +  * starting it  
 +  * resuming it 
 +  * pausing it 
 +  * stopping it  
 +  * and destroying the process that contains the activity.  
 + 
 +In this exercise we are going to see which //lifecycle callback methods// are called in different scenarios.  
 +For this override the callback methods in the //​MainActivity//​.  
 +As in the previous task you need to add a Log statement to every callback with its name as in the example: 
 + 
 +<code Java> 
 +    @Override 
 +    protected void onStart() { 
 +        super.onStart();​ 
 +        Log.i("​MainActivity",​ "​onStart"​);​ // You can choose any type of log level 
 +    } 
 +</​code>​  
 + 
 +What calls are done when: 
 +  - The application is launched 
 +  - The screen is rotated 
 +  - The application is moved in background 
 +  - The application is launched from background 
 +  - The application is destroyed
  
 === Task 6 - Use ADB to transfer files and install apps === === Task 6 - Use ADB to transfer files and install apps ===
  
-Connect to the mobile device/emulator using ADBADB can be used from a terminal on all operating systems.+[[https://developer.android.com/​studio/​command-line/​adb|Android Debug Bridge, ADB]]
  
-On Windows, ​the ADB executable ​can be found in the Android SDK Platform-Tools e.g. **D:​\Android\sdk\platform-tools\adb.exe**.+Connect to the mobile device/​emulator using //ADB//. //​ADB// ​can be used from a terminal on all operating systems.
  
-On Linux, the ADB executable can be installed ​by issuing the following command:+On Windows, the //ADB// executable can be found in the Android SDK Platform-Tools e.g. **D:​\Android\sdk\platform-tools\adb.exe**. 
 + 
 +On Linux, the //ADB// executable can be launched ​by issuing the following command:
  
 <​code>​ <​code>​
-sudo apt-get install ​adb+/​home/​student/​Android/​SDK/​platform-tools/adb
 </​code>​ </​code>​
  
-To get a list of connected devices use can use:+To get a **list of connected devices** use can use:
 <​code>​ <​code>​
 adb devices adb devices
 </​code>​ </​code>​
  
-To get a shell on the connected device use:+<​note>​If you have more than 1 device connected when you try to run a command you need to specify the device on which you want the command to run. //adb -s DEVICE_SERIAL <​command>//</​note>​ 
 + 
 +To get a **shell on the connected device** use:
 <​code>​ <​code>​
 adb shell adb shell
 </​code>​ </​code>​
  
-Android devices normally come with a more limited set of commands, so a text editor might not be available.+<​note>​Android devices normally come with a more limited set of commands, so a text editor might not be available.</​note>​
  
-Create a text file on your machine. Copy it to the connected device using:+Create a text file on your machine. ​**Copy it to the connected device** using:
 <​code>​ <​code>​
 adb push <​local>​ <​remote>​ adb push <​local>​ <​remote>​
 </​code>​ </​code>​
-where <​remote>​ represents the location on the device where you copy the file e.g. /sdcard/.+where <​remote>​ represents the location on the device where you copy the file e.g. **/sdcard/**.
  
-The file should be copied to the /sdcard/ partition on the phone. Connect to the device using adb shell, and use cat to check the contents of the file. +The file should be copied to the **/sdcard/** partition on the phone. Connect to the device using //adb shell//, and use //cat// to check the contents of the file. 
-To copy from the device use:+To **copy from the device** use:
 <​code>​ <​code>​
 adb pull <​remote>​ [<​local>​] adb pull <​remote>​ [<​local>​]
 </​code>​ </​code>​
  
-Both push and pull can be used with both directories and files.+Both //push// and //pull// can be used with both directories and files.
  
-To install an application through adb use:+To **install an application** through ​//adb// use:
 <​code>​ <​code>​
 adb install <​file>​ adb install <​file>​
 </​code>​ </​code>​
-Install LogLevel.apk (in the laboratory archive). At first if the installation fails due to a signature mismatch, make sure to uninstall the previous LogLevel application from the mobile device.+Install ​**LogLevel.apk** (in the laboratory archive). At first if the installation fails due to a signature mismatch, make sure to uninstall the previous LogLevel application from the mobile device.
  
  
-To check all available options of the ADB use:+To **check all available options of ADB** use:
 <​code>​ <​code>​
 adb --help adb --help
 </​code>​ </​code>​
  
smd/laboratoare/01.1519658578.txt.gz · Last modified: 2018/02/26 17:22 by vlad.traista
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0