Sunteți pe pagina 1din 12

-2014-

Facultatea de Automatica Calculatoare si Electronica


Universitatea din Craiova




Proiect la Sisteme Incorporate

Android Timer Example




Student : Sirbu Iulian Profesor: sl. dr. Nicoale Enescu







Descriere proiectului :
Aplicatia este un simplu cronometru. Acesta are controale simple, rapide si o interfaa intuitiv.
Poate fi personalizat n mod liber. Este exact, fiabil si foarte usor de utilizat .n acest exemplu, vom
crea o aplicaie simpl Android Timer. In realizarea acestei aplicatii vom utiliza unele idei i
instrumente de baz, cum ar fi Handler, pe care le putem folosi n multe cazuri, n aplicaiile noastre.
Vom folosi Handler pentru partea interesanta a acestui exemplu, care este actualizarea valorii
timerului.Pentru acest tutorial, vom folosi urmtoarele instrumente ntr-o platform Windows pe 64
de bii:
JDK 1.7
Eclipse 4.2 Juno
Android 4.2 SKD

1. Creai un nou proiect Android
Deschidem Eclipse IDE i selectam File -> New -> Project -> Android -> Project Android
Application. Trebuie s specificai numele aplicaie, numele proiectului i numele pachetelor n
cmpurile de text corespunztoare i apoi facei clic pe Urmtorul.



n fereastra urmtoare ne vom asigura ca opiunea "Create activity" este selectata, n scopul
de a crea o nou activitate pentru proiectul noastra, i facem clic pe Urmtorul. Acest lucru este
opional, deoarece putem crea o nou activitate si dup crearea proiectului, dar putei face totul
ntr-un singur pas.

Selectai "BlankActivity" i facei clic pe Urmtorul.

Ni se va cere s specificam anumite informaii despre noua activitate. n cmpul de text Name
Layout trebuie s specificam numele fiierului care va conine descrierea aspectului de aplicatiei . n
cazul nostru va fi creat res/layout/main.xml. Apoi, facem clic pe Finish.

2. Adugarea de resurse
Vom utiliza Package Explorer din Eclipse pentru a naviga la res/values/strings.xml






Cnd vom deschide fiierul strings.xml, Eclipse va afia resurse grafice View editor:

Acesta este un instrument frumos i uor pe care il putem utiliza pentru a aduga mai multe resurse
ca siruri de caractere, numere ntregi, valori de culoare, etc . Pentru aplicatia noastra noi vom lucra
n mod tradiional i vom edita fiierul strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Cronometru</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Cronometru</string>
<string name="timerVal">00:00:00</string>
<string name="timerMem">00:00:00</string>
<string name="pauseButtonLabel">Pauza</string>
<string name="resetButtonLabel">Stop</string>
<string name="startButtonLabel">Start</string>
<string name="MemButtonLabel">Memorare</string>
</resources>

Tocmai am creat unele resurse ir pe care le putem folosi n multe feluri i n multe locuri n aplicaia
noastr.
3. Crearea aspectului principal al aplicaiei
Acesta se face in fisierul res/layout/main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/samsung" >

<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="@string/timerVal"
android:textColor="#ffffff"
android:textSize="50sp" />

<TextView
android:id="@+id/timerMemory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="@string/timerMem"
android:textColor="#ffffff"
android:textSize="50sp" />

<Button
android:id="@+id/MemoryButton"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_alignLeft="@+id/resetButton"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/resetButton"
android:layout_marginBottom="48dp"
android:text="@string/MemButtonLabel" />

<Button
android:id="@+id/startButton"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_above="@+id/pauseButton"
android:layout_alignLeft="@+id/timerMemory"
android:layout_alignRight="@+id/timerMemory"
android:text="@string/startButtonLabel" />

<Button
android:id="@+id/resetButton"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_above="@+id/MemoryButton"
android:layout_alignLeft="@+id/pauseButton"
android:layout_alignRight="@+id/pauseButton"
android:layout_marginBottom="27dp"
android:text="@string/resetButtonLabel" />

<Button
android:id="@+id/pauseButton"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_above="@+id/resetButton"
android:layout_alignLeft="@+id/startButton"
android:layout_alignRight="@+id/startButton"
android:text="@string/pauseButtonLabel" />

</RelativeLayout>
4. Code
Codul aplicatiei
package com.example.androidtimer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button startButton;
private Button pauseButton;
private Button resetButton;
private Button MemoryButton;

private TextView timerValue;
private TextView timerMemory;

private long startTime = 0L;
private long resetTime = 0L;
private long memoryTime = 0L;

private Handler customHandler = new Handler();
private Handler customHandler2 = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

timerValue = (TextView) findViewById(R.id.timerValue);
timerMemory = (TextView) findViewById(R.id.timerMemory);

startButton = (Button) findViewById(R.id.startButton);

startButton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,
0);

}
});

pauseButton = (Button) findViewById(R.id.pauseButton);

pauseButton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View view) {

timeSwapBuff += timeInMilliseconds;

customHandler.removeCallbacks(updateTimerThread);


}
});

MemoryButton = (Button) findViewById(R.id.MemoryButton);

MemoryButton.setOnClickListener(new
View.OnClickListener() {

public void onClick(View view) {
memoryTime = updatedTime;
customHandler2.postDelayed(memTimerThread,
0);

}
});

resetButton = (Button) findViewById(R.id.resetButton);

resetButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

timeSwapBuff = resetTime;
timeInMilliseconds = resetTime;
updatedTime = resetTime;
customHandler.removeCallbacks(updateTimerThread);


}
});

}

private Runnable updateTimerThread = new Runnable() {

public void run() {

timeInMilliseconds = SystemClock.uptimeMillis() -
startTime;

updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);



}

};

private Runnable memTimerThread = new Runnable() {

public void run() {


int secs = (int) (memoryTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (memoryTime % 1000);
timerMemory.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}




5. Executarea aplicaiei

Acesta este ecranul principal al aplicaiei noastre. Aspectul ecranului principal este descris in
main.xml: