49
Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 1 bada 1.2.0 Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. bada Tutorial: Application

Bada tutorial.application

Embed Size (px)

DESCRIPTION

bada tutorial test

Citation preview

Page 1: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 1bada 1.2.0

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved.

bada Tutorial: Application

Page 2: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 2

Contents (1/2)

• Essential Classes

• Relationships between Classes

• Overview

• bada Applications

– Application Model

– Launching Application

– Terminating Application

• Application State Transition

– Application Life-cycle

– Foreground and Background

• System Events

• Application Framework

• Internationalization

• Launching Applications

Page 3: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 3

Contents (2/2)

• Notifications

• AppControl

– Application Control ID

– Operation ID

– Getting an AppControl

– AppControl Examples

• Examples:

– Create an Application

– Save and Restore Application Settings

– Handle Screen Events

– Use Sign-In Application Control

– Launch Other Applications and Handle Launch Arguments

– Example: Launching Applications Conditionally

– Example: Creating a Notification

– View New Application Details through Samsung Apps before Installation

• FAQ

Page 4: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 4

Feature Provided by

Provides an interface to the application frame. IAppFrame

Provides a listener for AppControl. IAppControlEventListener

Feature Provided by

Provides the base class to create applications. It initializes and terminates the application,

and also provides handlers for system events.Application

Provides localized application string resources. AppResource

Provides a repository to save and load user application‟s state and context. AppRegistry

Sends notifications to the user. NotificationManager

Provides application launching mechanism to use other application control features. AppControl

Finds the specific application control. AppManager

Essential Classes

• Interfaces

• Major classes

Page 5: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 5

Relationships between Classes

AppManager

FindAppControl()

AppControl

Start()

Application<<interface>>

IAppControlEventListener

OnAppControlCompleted()

<<interface>>

IAppFrame

AppRegistry

AppResource

Note: This is an external

class created by the

developer.

It does not belong to the

App namespace.

MyApplication

NotificationManager

Page 6: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 6

Overview

• The App namespace contains classes and interfaces that provide

the foundations for developing bada applications.

• Key features include:

– Application framework.

– Background and foreground event handling.

– Application Control.

Page 7: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 7

bada Applications

The application framework provides:

– Application life-cycle management:

• Initialization and termination

• Background and foreground handling

– Event handling:

• Battery and memory events

– Application control:

• Dial, contact, browser, media,

and messagingOperating System

Application Framework

Your Application

Page 8: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 8

Application Model (1/2)

In the bada platform, there are two application types:

– Base applications:

• Stored in the ROM and not removable via the Application manager.

• For example, Dialer, Contact, Camera, and Music player.

– bada applications:

• Installable and removable applications.

• In this document, the word “application” refers to a bada application.

Page 9: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 9

Application Model (2/2)

• A bada application running in the foreground is sent to the

background when base applications are launched, for example

when receiving an incoming voice call or user switching to home

screen.

• An application can utilize various device platform features provided by application controls through the AppControl class.

• bada platform task management

• Only one bada application can run at a time.

– If a bada application is already running when launching another bada

application, the first is always terminated by the platform.

• Multi-tasking is available between a bada application and base

applications. That is, one bada application can run simultaneously with

multiple base applications.

Page 10: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 10

Launching Application

• An application is launched when the user clicks the icon in the main menu.

• The system loads the necessary libraries and application executable binary to the memory.

• The application‟s entry point (OspMain()) creates an instance of the application and executes it.

• In the application initialization phase (OnAppInitializing()), resources, UI components, and previous application states can be loaded or initialized.

ApplicationMain menu

int OspMain()

Page 11: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 11

Terminating Application (1/2)

• The application closes for one of 2 reasons:

1. When the bada application itself calls the Application::Terminate() method, the application is terminated.

2. When the application is in the foreground, the user pressing the End

key terminates the application.

• The system forces the application to shut down:

1. If a second application launches while the first is running, the first

application is terminated. All bada applications follow a single bada

application policy, so only one bada application can run at a time.

2. When memory or battery power is extremely low, the system terminates

the application.

Page 12: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 12

Terminating Application (2/2)

Application termination tasks must be handled in the OnAppTerminating() method, which is called when the application terminates. When terminating:

– The application must close quickly.

– The application must free the resources it has been using to avoid memory leakage.

• The allocated UI control objects, such as the Frame, Form, and their child controls, are released before the OnAppTerminating() method is called. They are not accessible from within the OnAppTerminating() method.

– The application must close all pending connections to servers.

– The application can save its own state or context through AppRegistry. For instance, when the application is launched, it can resume from the last form from the previous execution.

– The forcedTermination argument specifies that the application is being terminated by the system. It is:

• True, when the application is forced to terminate, for example due to second bada application launching, low battery, or other system interruptions.False, when the termination is initiated by the user pressing the End key or closing the application from the task manager, or by the application calling Terminate() itself.

Page 13: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 13

Application State Transition (1/2)

Applications have the following states with corresponding event

handlers:

– Initializing

• Application framework initializes the application and creates the application‟s

frame.

• If the OnAppInitializing() method returns false, application state

changes to „Terminating‟ and it exits.

• If the OnAppInitializing() returns true, the application framework

proceeds to „Running‟ state.

– Running

– Terminating

• The application exits the event loop and frees its own resources.

Page 14: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 14

Application State Transition (2/2)

– Terminated

• It releases the control of the flow to the system.

- Application destroyed.

- Register application context in the

system.

- Create the application frame .

- Call the application‟s OnAppInitializing() method.

Initializing

Initialization

succeeds

Initialization fails

Termination

- Exit event loop.

- Call the application‟s OnAppTerminating()

method.

- Destroy allocated resources.

- Process events.

Create

Running

Terminating

Terminated

Page 15: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 15

Application Life-cycle

The diagram illustrates where

the application state events

occur in terms of the application

life-cycle.

Application life-cycle

OnAppInitializing()

OnForeground()

Running OnBackground()

OnAppTerminating()

Another window comes to the foreground

(for example, a system popup or another application)

ApplicationLaunch

Application comes tothe foreground

ApplicationExit

Showing splash image

Hiding splash image

Termination

Page 16: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 16

Foreground and Background (1/3)

Foreground:

– Applications are visible when they are in the foreground.

– When an application is first launched, the OnForeground()method is

called.

– An application is brought to the foreground from the background when:

• The running application is selected from the task manager.

• The application icon is selected in the main menu.

– When the application is brought to the foreground:

• Resume graphics processing (3D or animation) since the application now

has focus.

• Resume the operations which were stopped when the application was last

moved to the background.

– When the system needs to refresh a screen component, it can call the OnForeground()method even if the application is present in the

foreground.

Page 17: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 17

Foreground and Background (2/3)

Background:

– Applications are not visible when they are in the background.

– An application moves to the background when :

• The Home key is pressed.

• Other windows pop up, such as windows for incoming calls and alarms.

– When the application is sent to the background:

• Stop graphics processing (3D, animation, etc.) since they will not be

displayed anyway.

• Release unnecessary resources.

• Stop media processing, haptic and sensors manipulations.

-OnBackground() is called-OnForeground() is calledForeground

- Home key pressed

- Other windows pop up

-Selected from the task manager

- Icon pressed in the main menu

Background

Page 18: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 18

Foreground and Background (3/3)

The following diagram illustrates the various events and states from

the application and the application framework‟s perspective.

Page 19: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 19

System Events (1/3)

System events must be properly handled by the Application class event

handlers:

• Battery event: Applications that use multimedia resources, such as the

camera and media player, need to check the battery level in the OnBatteryLevelChanged() event handler:

– If the level is EMPTY, terminate the application.

– If the level is CRITICAL, stop using multimedia features, since they are not

guaranteed to function properly at this battery level.

• Memory event: When memory is low, the OnLowMemory() event

handler is called. Free unused memory from the heap in this event

handler.

Page 20: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 20

System Events (2/3)

Screen event:

– Applications can receive screen events in OnScreenOn() and OnScreenOff()

event handlers if they register a screen event listener. When theOnScreenOff() event handler is triggered, the application must reduce power

consumption by releasing the activated resources, such as 3D, multimedia, and

sensors, which are no longer used. The released resources can be acquiredagain when the OnScreenOn() event handler is triggered.

– The resources must be handled efficiently by the OnForeground()/

OnBackground() and OnScreenOn()/OnScreenOff() event handlers.

Be careful not to duplicate or delete resources.

– When the device is in auto lock mode, OnBackground()is called directly

after OnScreenOff().

– The following events are triggered when the backlight time expires:

• OnScreenOff()

• If device is in auto lock mode, OnBackground() is called after OnScreenOff() when

the lock UI is displayed.

Page 21: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 21

System Events (3/3)

– The following events are triggered when the unlock key is pressed.

• If the touch lock is enabled, OnScreenOn() is triggered.

• OnForeground() is called after the lock UI disappears.

– When the lock key is pressed explicitly, only OnBackground() is called

after the lock UI is displayed. When the backlight turns off, OnScreenOff() is

called.

Page 22: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 22

Application Framework

• Application:– All applications must derive from

Application class

• AppManager:

– Application manager class

– Finds an AppControl instance

• IAppFrame:

– UI frame interface

– Frame is the main window in an application

• AppResource:

– Application resource class

– Can contain localized strings

• AppControl:

– Application control class

– Used to start an application and control specific behavior

• NotificationManager:

– Notification manager class

– Used to alert the user about notifications

AppControl

IAppFrame

AppManagerApplication

AppRegistry

AppResource

NotificationManager

Page 23: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 23

Internationalization (1/2)

• String resource is supported for multiple languages. Each localized string is saved to each xml file in Res folder.

– For example: eng-GB.xml, fra-FR.xml, ita-IT.xml,

deu-DE.xml, kor-KR.xml

[IDE String Resource]

• AppResource handles localized string resource based on preferred

display language that is defined in the device settings.

AppResource

eng-GB.xml

“Hello”

Your App

Menu Back

Form

Language Setting

Page 24: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 24

Internationalization (2/2)

Retrieving localized strings

1. Prepare the localized strings table with the UI Builder. For more

information, see Using String Tables in the Development Environment

tutorial.

2. Get the localized string from the AppResource class.

AppResource retrieves the chosen language from the display

language settings and returns the localized strings.

– AppResource::GetString(const String& id, String&

loadedString)

Application* pApp = Application::GetInstance();

String str1;

r = pApp->GetAppResource()->GetString("IDS_STRING1", str1);

Page 25: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 25

Launching Applications (1/2)

Launching other applications:

– A bada application can launch other applications using the AppManager::LaunchApplication()method.

– The AppManager::LaunchApplication() method requires the

SYSTEM privilege level, and to use the method, you must assign the

APP_SERVICE privilege group to your application.

– All bada applications follow the single application policy. Therefore, the OnAppTerminating() method is called immediately after the

LaunchApplication() method, and the calling application is

terminated.

– You can retrieve the launch arguments from an application by using the Application::OnUserEventReceivedN() method. For more

information, see the API Reference.

Page 26: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 26

Launching Applications (2/2)

Launching applications with conditions:

– A bada application can launch itself with a specified condition using the AppManager::RegisterAppLaunch() method with the SYSTEM

privilege level and APP_SERVICE privilege group.

– The application is newly launched when the condition is fulfilled, unless

the application is already running. If the application is running, a

notification is delivered in the Application::OnUserEventReceivedN() event hander.

– A launch condition can be specified as follows:

Type Condition format Description

Due time L”DataTime=„mm/dd/yyyy hh:mm:ss‟” Launch at the local time

„mm/dd/yyyy hh:mm:ss‟ .

The time format matches the output

format of

Base::DateTime::ToString().

Serial L”Serial=„command‟” Launch on the serial communication

input „command‟.

Page 27: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 27

Ticker Quick panel Badge number Sound

Notifications

• An application can notify the user using the NotificationManager::Notify() method.

• The NotificationManager::Notify() method requires the

NOTIFICATION privilege group. In addition, you must define an icon

for the ticker or quick panel in the application properties.

• A notification can be a combination of the following types:

Page 28: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 28

AppControl (1/2)

• AppControl is a standard mechanism for using specific

operations exported by base applications

• AppControl can communicate data and get back results.

• AppControl instance can be created from the AppManager.

• AppManager finds the service providing application control and

returns it for your application to use.

• AppControls can be identified using 2 parameters:

– Application Control ID:

• Defines the variable for identifying each application control instance. For example, APPCONTROL_DIAL.

– Operation ID:

• Defines the variable for identifying the behavior of each application control instance. For example, OPERATION_VIEW.

Page 29: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 29

AppControl (2/2)

• The result of the application control‟s execution is returned by IAppControlEventListener‟s OnAppControlCompleted()

method.

– OnAppControlCompleted() is invoked when an application finishes

its operation and a control callback event occurs.

• Some AppControls require privileges to use them:

– For example, Call and Browser requires Normal privilege.

Page 30: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 30

AppControl name Operation ID Description

APPCONTROL_CONTACT OPERATION_PICK This operation can be used to read the phone number, email address, contact ID,

or vCard file path for one or more contacts in the contact database.

OPERATION_ADD This operation can be used to add a contact to the contact database. The

parameters passed to this AppControl are automatically filled in the appropriate

fields in the Add Contact UI.

OPERATION_EDIT This operation can be used to edit a contact in the contact database. The

parameters passed to this AppControl are automatically filled in the appropriate

fields in the Edit Contact UI.

OPERATION_VIEWThis operation can be used to view a vCard file.

The contact details are displayed only if the vCard file path is specified.

APPCONTROL_CALENDAR OPERATION_PICK This operation can be used with either the single or the multiple selection feature

to select one or more Calendar application items, such as events, to-do items, or

memos. Event and to-do items are returned as .vcs files and memo items

as .vnt files.

OPERATION_VIEW

This operation can be used to view a vCalendar file or a calendar event.

The event details are displayed only if the vCalendar (event) file path or the record ID of the event (used in Social::Calendarbook) is specified. The to-do item

details are displayed only if the vCalendar (to-do) file path is specified.

APPCONTROL_DIAL OPERATION_DEFAULT This application control can be used to launch the Dial UI. The parameter passed

to this application control is displayed on the screen. Users can edit this number

and make calls using the Call key.

APPCONTROL_CALL OPERATION_DEFAULT This operation can be used to call the specified number. Using this AppControl

requires special privileges.

APPCONTROL_BROWSER OPERATION_DEFAULT This operation can be used to open a Web browser using the specified URL.

Using this AppControl requires special privileges.

Application Control ID (1/3)

Page 31: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 31

AppControl name Operation ID Description

APPCONTROL_SIGNIN OPERATION_SIGNIN This operation can be used to sign in to the bada Server. After the user has

signed in for the first time, this operation handles all subsequent sign-ins

automatically. Using this AppControl requires special privileges.

OPERATION_SIGNOUT This operation can be used to sign out from the bada Server. This operation

signs out without displaying any dialog. Using this AppControl requires

special privileges.

APPCONTROL_CAMERA OPERATION_CAPTURE This operation can be used to launch the Camera UI to capture images or

record video.

APPCONTROL_MESSAGE OPERATION_EDIT This operation can be used to compose SMS or MMS messages. The

parameters passed to this application control are automatically filled in the

appropriate fields in the displayed message composer.

APPCONTROL_EMAIL OPERATION_EDIT This operation can be used to compose email messages. The parameters

passed to this application control are automatically filled in the appropriate

fields in the displayed message composer.

APPCOTNROL_BT OPERATION_PICK This operation can be used to obtain the details of a Bluetooth profile on the

device.

Application Control ID (2/3)

Page 32: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 32

AppControl name Operation ID Description

APPCONTROL_MEDIA OPERATION_PICK This operation can be used to select one or more media files , such as images

or video recordings, from the My Files folder.

APPCONTROL_IMAGE OPERATION_VIEW This operation can be used to view image files on the device.

APPCONTROL_VIDEO OPERATION_PLAY This operation can be used to play video files on the device.

APPCONTROL_AUDIO OPERATION_PLAY This operation can be used to play audio files on the device.

APPCONTROL_SETTINGS OPERATION_DEFAULT This operation can be used to view the settings of a specific feature category.

Currently, only the location category is supported.

Application Control ID (3/3)

Page 33: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 33

Operation ID

Operations specify the specific behavior or action that must be

performed.

Operation ID Description

OPERATION_ADD Adds an item or content to a specific AppControl.

OPERATION_CAPTURE Captures an item or content in a specific AppControl.

OPERATION_EDIT Edits an item or content in a specific AppControl.

OPERATION_DEFAULT Launches the AppControl without any operation.

OPERATION_PICK Obtains data from a specific AppControl.

OPERATION_PLAY Plays an item or content in a specific AppControl.

OPERATION_VIEW Displays an item or content in a specific AppControl.

OPERATION_SIGNIN Displays a sign-in window.

OPERATION_SIGNOUT Signs out of a session.

Page 34: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 34

Getting an AppControl

bada

Application

AppControls

Registry

AppControl

AppManager

[AppControl Resolver]

#Dial

type=“…”

Path=“…”

…#Browser

type=“…”

Path=“…”

#SignIn

type=“…”

Path=“…”

type=“…”

Path=“…”

#Video

type=“…”

Path=“…”

Base Application

(For example, Contact)

AppControl

4. Launch the application AppControl

ID and operation ID.

2 Resolve the AppControl ID and operation ID.

1. Find AppControl.

IAppControlEventListener

OnAppControlCompleted()

5. Asynchronously send result to

AppControl event listener.

3. Start AppControl.

Page 35: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 35

Media

Application

AppControl Examples

• APPCONTROL_MEDIA:

– Launches the media

application to select audio,

video, or image content from

the Media folder.

– Receives selections in an

event handler.

Caller

Application

AppControl ID: APPCONTROL_MEDIA

Operation ID: OPERATION_PICK

Option: “type:video”

“selectionType:single”

The calling application receives information of the

selected media, i.e. URI.

Caller

Application

Dialer

Application

AppControl ID: APPCONTROL_DIAL

Operation ID: NONE

Option: “tel:+82312798707”

• APPCONTROL_DIAL:

– Launches the dialer

application with a pre-filled

phone number.

– Requires normal privilege.

Page 36: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 36

Example: Create an Application (1/2)

This example illustrates how application default methods are created.

– Open \<BADA_SDK_HOME>\Examples\UIApplication\src

\HelloWorld.cpp.

1. Create the HelloWorld application code using the Form Based

Application template.

Page 37: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 37

Example: Create an Application (2/2)

Notes

– Applications must derive from the Application class.

– You must create and download your application‟s manifest.xml file from

bada Developers. In addition to the information above (name, ID, secret

code), it contains other information necessary for your application, such

as privileges and device profiles.

Application NameGetAppName()

Application IDGetAppId()

Secret CodeGetAppSecret()

Manifest.xml

Application

MyApplication

Page 38: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 38

Example: Save and Restore Application Settings (1/2)Save and restore application state.

1. In the OnAppInitializing() method, use AppRegistry to

retrieve application settings: AppRegistry::Get(key, value)

2. The first time an application runs, the registry entry must be added: AppRegistry::Add(key, value)

3. Save application settings to disk in the registry: AppRegistry::Save()

4. Set registry values in the OnAppTerminating() event handler:

AppRegistry::Set(key, value)

Page 39: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 39

Example: Save and Restore Application Settings (2/2)

bool MyApplication::OnAppTerminating(AppRegistry&

appRegistry)

{

result r = E_SUCCESS;

r = appRegistry.Set("LastMessage", "Who destroyed");

//enum { INIT=0, PLAYING, SCORE } GameStatus =

PLAYING;

appRegistry.Set("GameStatus", (int)GameStatus);

appRegistry.Save();

}

bool MyApplication::OnAppInitializing(AppRegistry&

appRegistry)

{

String message("NONE");

r = appRegistry.Get("LastMessage", message);

if(r == E_KEY_NOT_FOUND

{

appRegistry.Add("LastMessage", message);

}

// Enumeration definitions are defined

elsewhere.

// enum { INIT=0, PLAYING, SCORE } GameStatus

= INIT;

int gamestatus = GameStatus;

r = appRegistry.Get("GameStatus", gamestatus);

if(r == E_KEY_NOT_FOUND

{

appRegistry.Add("GameStatus", gamestatus);

}

appRegistry.Save();

}

Page 40: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 40

This example illustrates how to

handle a sound resource during

screen on and screen off events.

You can handle other resources

in the same way.

1. Implement theScreenEventListener to

handle screen events.

2. Check the sound status

using the boolean value of the IsPlaying variable.

3. Play or stop the sound using PlaySound()or

StopSound() accordingly.

Example: Handle Screen Events

bool MyApplication::OnAppInitializing(AppRegistry&

appRegistry)

{

PowerManager::SetScreenEventListener(*this);

}

void MyApplication::OnScreenOn (void)

{

// Check if sound is off, then turn on sound.

if(!IsPlaying)

PlaySound();

}

void MyApplication::OnScreenOff (void)

{

if(IsPlaying)

StopSound();

}

void MyApplication::OnForeground()

{

if(!IsPlaying)

PlaySound();

}

void MyApplicaiton::OnBackground()

{

if(IsPlaying)

StopSound();

}

Page 41: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 41

Example: Use Sign-In Application Control

1. Get an AppControl instance by calling the FindAppControlN()

method. Provide the SignIn Control ID as parameter: AppManager::FindAppControlN(APPCONTROL_SIGNIN,

OPERATION_SIGNIN)

2. If the FindAppControlN() method returns an AppControl instance,

start the AppControl by calling its Start() method:

AppControl::Start(null, this)

Notes:

– If you require event handlers, implement an IAppControlEventListener.

AppControl* pAc = AppManager::FindAppControlN(APPCONTROL_SIGNIN, OPERATION_SIGNIN);

if(pAc != null)

{

pAc->Start(null, this);

delete pAc;

}

else

AppLog("AppControl Not Found. \n");

Page 42: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 42

Example: Launch Other Applications and Handle Launch Arguments (1/2)

This example illustrates how to launch other applications from a bada

application and retrieve the launch arguments.

1. A caller application launches another application with arguments using the LaunchApplication() method.

2. The OnAppTerminating()method is called, terminating the caller

application.

3. The called application is launched. If the launch arguments exist, requestId is assigned as a reserved launch ID

(AppLaunchRequestId).

Page 43: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 43

Example: Launch Other Applications and Handle Launch Arguments (2/2)

AppId calleeAppId = L”1234567890”;

AppManager *pAppManager = AppManager::GetInstance();

ArrayList *pArgList = new ArrayList();

pArgList->Construct();

String *aArg = new String(L”yourdata”);

pArgList->Add(*aArg);

r = pAppManager->LaunchApplication(calleeAppId, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);

Caller application:

void CalleeApp::OnUserEventReceivedN (RequestId requestId, Osp::Base::Collection::IList *pArgs)

{

if(requestId == AppLaunchRequestId) // If launch arguments exist

{

if(pArgs)

{

for(int i = 0; i < pArgs->GetCount(); i++)

AppLog("pData[%d]=%S", i, ((String*)(pArgs->GetAt(i)))->GetPointer());

// pArgs[0] represents the launch type (for example, APP_LAUNCH_NORMAL value)

// pArgs[1] represents the operation of the launch (default value is OPERATION_DEFAULT).

// pAgrs[>=2] contains the actual arguments sent from the caller (for example, “yourdata”)

}

else // Handling User Events

Called application:

Page 44: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 44

This example illustrates how to

launch an application

conditionally at a given time. The

same launch argument scheme

can be exploited as the LaunchApplication()

method.

1. Define the condition in the proper String format.

2. Register the application for the launch with arguments using the RegisterAppLaunch()

method.

3. The registered application is

launched at the defined time.

Example: Launching Applications Conditionally

DateTime time;

SystemTime::GetCurrentTime(WALL_TIME, time);

Time.AddMinutes(1);

String condition;

condition.Format(40, L”DateTime=„%S‟”,

time.ToString().GetPointer());

ArrayList* pArgList = newArrayList();

pArrayList->Construct();

String *aArg = new String(L”yourdata”);

pArgList->Add(*aArg);

r = AppManager::GetInstance()-

>RegisterAppLaunch(condition, pArgList,

AppManager::LAUNCH_OPTION_DEFAULT);

Page 45: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 45

Example: Creating a Notification

This example illustrates how to inform the user with a ticker and

notification.

1. Construct an instance of the NotificationManager class.

2. If you want to send a badge notification, handle the badge number

properly.

3. Send the notification.

NotificationManager* pNotiMgr = new NotificationManager();

pNotiMgr->Construct();

Int badgeNumber = pNotiMgr->GetBadgeNumber();

badgeNumber++;

r = pNotiMgr->Notify(L”A new message has arrived.”, badgeNumber);

Page 46: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 46

Example: View New Application Details through Samsung Apps before Installation (1/2)

This example illustrates how you can launch the SamsungApps

application to view the details of a new application on offer at Samsung

Apps before deciding to download and install the new application. The

SamsungApps application provides users easy access to new

applications.

1. A caller application retrieves the SamsungApps application ID using the SystemInfo::GetValue()method.

2. The caller application launches the SamsungApps application with the LaunchApplication() method, using the application ID of the

new application you want to view as a parameter.

3. The OnAppTerminating()method is called, terminating the caller

application.

4. The SamsungApps application is launched and displays the details

of the new application.

Page 47: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 47

AppId samsungAppsAppId;

Osp::System::SystemInfo::GetValue("SamsungAppsAppId", samsungAppsAppId);

AppManager *pAppManager = AppManager::GetInstance();

ArrayList *pArgList = new ArrayList();

pArgList->Construct();

String *aArg = new String(L”1234567890”); // ID of the new application whose details you want to view

pArgList->Add(*aArg);

r = pAppManager->LaunchApplication(samsungAppsAppId, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);

Caller application:

Example: View New Application Details through Samsung Apps before Installation (2/2)

Page 48: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 48

FAQ

• Where do I initialize and free-up application resources?

– Initialize them in the OnAppInitializing() method.

– Free non UI resources in the OnAppTerminating() method, but UI

resources(e.g. form and its child controls) is freed automatically when

the application closed. You do not need to free them explicitly.

Please review the UI parts of this document about UI resource.

• Do I have to set my application‟s ID somewhere?

– No, an application ID (a unique identifier bound to a bada application) is

created from the bada developer site and saved into the application manifest file (manifest.xml).

The bada IDE does not automatically generate the GetAppId(),

GetAppName(), and GetAppSecret() methods, since they are

member methods of the Application class.

Page 49: Bada tutorial.application

Copyright © 2010 Samsung Electronics Co., Ltd. All rights reserved. 49