Опубликован: 22.12.2015 | Уровень: для всех | Доступ: платный
Лекция 35:

Состояние батареи

< Лекция 34 || Лекция 35 || Лекция 36 >
Ключевые слова: файл, функция, контейнер, box, int, API

Запрос состояния батареи

Создайте новый проект ‘BatteryInfo.’ Откройте файл исходного кода и введите следующий код в начале файла.

#include "batteryinfo.h"
#include <device/battery.h>
#include <device/callback.h>

device/battery.h — это файл заголовка библиотеки, содержащий информацию о батарее.

device/callback.h это - файл заголовка библиотеки обработчика событий, связанного с этим устройством.

Добавим новую функцию перед create_base_gui(). Эта функция добавит элемент управления в контейнер Box.

static void
my_box_pack(Evas_Object *box, Evas_Object *child, double h_weight, double v_weight, double h_align, double v_align)
{
/* create a frame we shall use as padding around the child widget */
Evas_Object *frame = elm_frame_add(box);
/* use the medium padding style. there is "pad_small", "pad_medium",
* "pad_large" and "pad_huge" available as styles in addition to the
* "default" frame style */
elm_object_style_set(frame, "pad_medium");
/* set the input weight/aling on the frame insted of the child */
evas_object_size_hint_weight_set(frame, h_weight, v_weight);
evas_object_size_hint_align_set(frame, h_align, v_align);
{
/* tell the child that is packed into the frame to be able to expand */
evas_object_size_hint_weight_set(child, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
/* fill the expanded area (above) as opposaed to center in it */
evas_object_size_hint_align_set(child, EVAS_HINT_FILL, EVAS_HINT_FILL);
/* actually put the child in the frame and show it */
evas_object_show(child);
elm_object_content_set(frame, child);
}
/* put the frame into the box instead of the child directly */
elm_box_pack_end(box, frame);
/* show the frame */
evas_object_show(frame);
}

Затем добавьте код в функцию create_base_gui().

/* Conformant */
ad->conform = elm_conformant_add(ad->win);
elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE);
evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(ad->win, ad->conform);
evas_object_show(ad->conform);

{ /* child object - indent to how relationship */
/* A box to put things in verticallly - default mode for box */
Evas_Object *box = elm_box_add(ad->win);
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_object_content_set(ad->conform, box);
evas_object_show(box);
{ /* child object - indent to how relationship */
/* Label*/
ad->label = elm_label_add(ad->win);
elm_object_text_set(ad->label, "<align=center>Hello Tizen</>");

/* expand horizontally but not vertically, and fill horiz,
* align center vertically */
my_box_pack(box, ad->label, 1.0, 0.0, -1.0, 0.0);
/* Button-1 */
Evas_Object *btn = elm_button_add(ad->win);
elm_object_text_set(btn, "Default style");
evas_object_smart_callback_add(btn, "clicked", show_battery_state, ad);
/* epand both horiz and vert, fill horiz and vert */
my_box_pack(box, btn, 1.0, 1.0, -1.0, 0.0);
}
}
/* Show window after base gui is set up */
evas_object_show(ad->win);

Добавьте обработчик событии кнопки выше функции create_base_gui().

static void
show_battery_state(void *data, Evas_Object *obj, void *event_info)
{
appdata_s *ad = data;
int result=0, percent=0;
bool charging = false;
device_battery_get_percent(&percent);
device_battery_is_charging(&charging);
char buf[100];
sprintf(buf, "Battery Remain : %d %% - %s", percent, charging ? "charging" : "uncharging");
elm_object_text_set(ad->label, buf);
}

device_battery_get_percent(int *) - это API, который возвращает уровень заряда батареи в процентах.

device_battery_is_charging(bool *) - это API , который возвращает степень заряда.

Запустите пример


< Лекция 34 || Лекция 35 || Лекция 36 >
Тимур Имашев
Тимур Имашев
Россия, Кемерово, КемГИК
Александр Бураков
Александр Бураков
Россия, г.Тюмень, Тюменская область