You get a bonus - 1 coin for daily activity. Now you have 1 coin

12. Logs and pop-up messages

Lecture



In this lesson we:

- consider the application logs and pop-up messages

Create a project:

Project name : P0121_LogAndMess
Build Target : Android 2.3.3
Application name : LogAndMess
Package name : ru.startandroid.develop.logandmess
Create Activity : MainActivity

Create a screen in main.xml , familiar to us from past lessons about handlers:


12. Logs and pop-up messages

The algorithm of the application will be the same. By pressing the buttons text changes. Handler - Activity.

public class MainActivity extends Activity implements OnClickListener {

TextView tvOut;
Button btnOk;
Button btnCancel;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// найдем View-элементы
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);

// присваиваем обработчик кнопкам
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// по id определяем кнопку, вызвавшую этот обработчик
switch (v.getId()) {
case R.id.btnOk:
// кнопка ОК
tvOut.setText("Нажата кнопка ОК");
break;
case R.id.btnCancel:
// кнопка Cancel
tvOut.setText("Нажата кнопка Cancel");
break;
}
}

}

Save, run. Make sure everything works.

Application logs

When you test the application, you can see the logs of work. They are displayed in the LogCat window. To display the window, open the menu Window> Show View> Other ... In the window that appears, select Android> LogCat

12. Logs and pop-up messages

A LogCat tab should appear.

12. Logs and pop-up messages

Consider this tab in more detail. Logs have different levels of importance: ERROR , WARN , INFO , DEBUG , VERBOSE (descending). VDIWE buttons (in circles) are filters and correspond to log types. Try them and note that the filter shows logs not only of its level, but also of higher importance levels. You can also create, edit and delete your filters - we will look at this a little further.

Let's watch how to write logs. This is done quite easily with the Log class and its methods Log.v () Log.d () Log.i () Log.w () and Log.e (). The names of the methods correspond to the level of the logs they write.

Change the code MainActivity.java . Take all the codes from the code and add to the DEBUG logs using the Log.d method. The method requires an input tag and message text . A tag is something like a label, so that later it would be easier to find our message in a heap of system logs. Add a tag description (TAG) and write all the texts of the tags to the log.

public class MainActivity extends Activity implements OnClickListener {

TextView tvOut;
Button btnOk;
Button btnCancel;

private static final String TAG = "myLogs";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// найдем View-элементы
Log.d(TAG, "найдем View-элементы");
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);

// присваиваем обработчик кнопкам
Log.d(TAG, "присваиваем обработчик кнопкам");
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// по id определяем кнопку, вызвавшую этот обработчик
Log.d(TAG, "по id определяем кнопку, вызвавшую этот обработчик");
switch (v.getId()) {
case R.id.btnOk:
// кнопка ОК
Log.d(TAG, "кнопка ОК");
tvOut.setText("Нажата кнопка ОК");
break;
case R.id.btnCancel:
// кнопка Cancel
Log.d(TAG, "кнопка Cancel");
tvOut.setText("Нажата кнопка Cancel");
break;
}
}

}

Eclipse curses that it does not know the class Log. Update the import (CTRL + SHIFT + O) and, if asked, select android.util.Log . Run the application, click the buttons and see the logs

12. Logs and pop-up messages

It is evident that everything is fine recorded. To make viewing more convenient, create your own filter. Click the + icon

12. Logs and pop-up messages

The filter name is arbitrary, for example, “ My logs ”. Log Tag is just the value of the TAG constant, which is described in our code and used in the Log.d method, i.e. - " myLogs ". Pid is left empty , this is the process id. Level set Debug

12. Logs and pop-up messages

and click OK. There is a new tab My logs, which displays logs corresponding to the newly created filter.

We put the text in the log, but of course, you can write, for example, the values ​​of the variables you are interested in (cast to the String type).

Sometimes it happens that the logs are not displayed in the LogCat tab, although AVD is running, the application works without problems. In this case, the following should help: in Eclipse, go to the menu Window> Open Perspective> Other> DDMS. A slightly different set of windows will open. There, find the Devices tab and your AVD device should be visible in it, click on it and the logs should appear. To return to development: Window> Open Perspective> Java.

Pop up messages

An application can show pop-up messages using the Toast class. Let's edit the onClick method. Let's make it so that a message pops up about which button was pressed.

public void onClick(View v) {
// по id определяем кнопку, вызвавшую этот обработчик
Log.d(TAG, "по id определяем кнопку, вызвавшую этот обработчик");
switch (v.getId()) {
case R.id.btnOk:
// кнопка ОК
Log.d(TAG, "кнопка ОК");
tvOut.setText("Нажата кнопка ОК");
Toast.makeText(this, "Нажата кнопка ОК", Toast.LENGTH_LONG).show();
break;
case R.id.btnCancel:
// кнопка Cancel
Log.d(TAG, "кнопка Cancel");
tvOut.setText("Нажата кнопка Cancel");
Toast.makeText(this, "Нажата кнопка Cancel", Toast.LENGTH_LONG).show();
break;
}
}

Let us examine the syntax of the call. The static makeText method creates a View-element Toast. Method parameters:

- context - until we go into details of what it is and use the current Activity, i.e. this.
- text - the text to show
- duration - the duration of the show (Toast.LENGTH_LONG - long, Toast.LENGTH_SHORT - short)

Toast is created and the show () method is called to display it. Save, run, check.

12. Logs and pop-up messages

If you have an Android smartphone, I think you have already seen similar messages. Now you know how to do it)

In the next lesson:

- create menu items


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)