Міністерство освіти і науки України
Національний університет “Львівська політехніка”
/
Звіт
До Лабораторної роботи №2
З дисципліни: «Мережні операційні системи»
На тему: «Управління обчисленнями в ОС Android»
МЕТА: Оволодіти навичками роботи з Android Activity та Activity stack.
1. Загальні відомості
1.1. Основні поняття: Context, Activity, Intent, Service [1,2,3].
1.2. Використання Context [1,2,3].
1.3. Використання Activity [1,2,3].
1.4. Activity stack [1,2,3].
1.5. Життєвий цикл Android Activity [1,2,3].
1.6. Методи onCreate(), onStart(), onResume(), onPause(), onRestart(), onStop(), onDestroy()[1,3].
1.7. Призначення та використання Intent [1,2,3].
Хід виконання програми
1. Програма виводу етапів життєвого циклу Android Activity (Listing 4–1. AndroidBasicsStarter.java + Listing 4–2. LifeCycleTest.java [4])
MainActivity.java
package com.example.user.lab2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String TAG = "Life cycle";
private TextView mInfoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInfoTextView = (TextView) findViewById(R.id.textViewInfo);
Toast.makeText(getApplicationContext(), "onCreate()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "onStart()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onStart()");
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "onResume()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "onPause()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onStop()");
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "onRestart()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onRestart()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy()", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onDestroy()");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonTouchMe:
mInfoTextView.setText("Success");
break;
case R.id.buttonExit:
finish();
break;
default:
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.lab2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="365dp"
android:layout_height="482dp"
android:orientation="vertical"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/textViewInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/before_touch"
android:textSize="24sp" />
<Button
android:id="@+id/buttonTouchMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/touch_me" />
<Button
android:id="@+id/buttonExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/exit" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
/
Рис. 1. Виконання програми
Висновок: на даній лабораторній роботі я оволодів навичками роботи з Android Activity та Activity stack.