语言

Menu
Sites
Language
Kill Background Apps

How to kill background apps in Tizen studio ?

I have added privilege for the appmanager.kill.bgapp and used app_control_send_terminate_request. But the app is not terminated from the Long press Home screen.

响应

6 回复
Shaswati Saha

Hi KISHORE,

I've tried to implement a simple scenario i.e. tried to stop a service(background) app from an UI app. You may try following the way I've followed.

What I did, simply launched a service app from an UI app and printed a log in every 1s interval for test purpose that the service app is alive. And after that against the click event applied on a label in my UI app I've registered a callback which exits the service app and thus stops the 1s interval log printing. My code is as below:

In the UI app:

static void
create_base_gui(appdata_s *ad)
{
... ...
app_control_h app_control;
app_control_create(&app_control);
app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
app_control_set_app_id(app_control, "SERVICE_APP_ID");
app_control_add_extra_data(app_control, "app", "logPrint"); //added an extra data to realize whether to exit or print log!
app_control_send_launch_request(app_control, NULL, NULL);
app_control_destroy(app_control);

... ...
}

Callback registered for the click event:

static void _clicked(void *user_data, Evas_Object *obj, void *event_info)
{
    app_control_h app_control;
	app_control_create(&app_control);
	app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
	app_control_set_app_id(app_control, "SERVICE_APP_ID");
	app_control_add_extra_data(app_control, "app", "stopLogprint");
	app_control_send_launch_request(app_control, NULL, NULL);
	app_control_destroy(app_control);

}

In the service_app_control:

Eina_Bool
logPrint(void *data EINA_UNUSED)
{

    dlog_print(DLOG_INFO, LOG_TAG, "Hello from Service App");
	return EINA_TRUE;
}

void service_app_control(app_control_h app_control, void *data)
{
	char *app = NULL;
	char *operation = NULL;
	app_control_get_operation(app_control, &operation);

		if(!strcmp(operation, APP_CONTROL_OPERATION_DEFAULT))
		{
			dlog_print(DLOG_INFO, LOG_TAG, "before exit: In app_control");
			app_control_get_extra_data(app_control, "app", &app);
			dlog_print(DLOG_INFO, LOG_TAG, "app_id %s", app);

			if(!strcmp(app, "stopLogprint")) service_app_exit();
			ecore_timer_add(1, logPrint, NULL);
		}
    return;
}

Hope it'll work!

KISHORE BATTA

Hi Shaswati Saha,

Thanks for your answer.

i'm not getting where did you invoke the functions,

_clicked
logPrint
service_app_control

Consider settings app as an example,

In create_base_gui function, after app_control_send_launch_request, it will open the settings app.

After that, i did not understand .. Can you give me clear explanation ?

Thanks.
 

Shaswati Saha

_clicked is a callback which is invoked when clicked on the label. Please have a look into the code below. It is registered as a callback against a click event.

ad->label = elm_label_add(ad->conform);
elm_object_text_set(ad->label, "<align=center>Hello Tizen</align>");
evas_object_size_hint_weight_set(ad->label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_smart_callback_add(ad->label, "clicked", _clicked, ad);
elm_object_content_set(ad->conform, ad->label);

logPrint callback is invoked from the ecore_timer_add() function. Please try finding the below line of code in my previous reply.

ecore_timer_add(1, logPrint, NULL);

service_app_control() is automatically called. It is a part of service app lifecycle. Please have a look into this link.

KISHORE BATTA

Hi Shaswati Saha,

service_app_control is called when the service application receives an app_control service request from another application.

When i click the _clicked from the UI app, the service app opens. But the service_app_control function is not getting invoked. I tried to check the logs using  :  dlogutil LOG_TAG:I in the terminal. Nothing is printed.

Will the app get terminated from the task switcher ?

KISHORE BATTA

Logs are printitng now. But if the process is termianted, the pid should not exist. But i can see the pid of the service app in cd /proc/<pid>.. I think the process is still running.

Shaswati Saha

Invoking service_app_exit() function immediately stops the service app and the Pid doesn't exist in my case. Would you please share the place from where you're getting confirmed of the existance of Pid? I couldn't see that in Log after clicking on the label of the UI app in my case.

I've modified the service_app_control() function as below. Would you please recheck it?

void service_app_control(app_control_h app_control, void *data)
{
	int ret;
	char *app = NULL;
	char *operation = NULL;
	app_control_get_operation(app_control, &operation);
	app_control_get_extra_data(app_control, "app", &app);

	if(app!=NULL && !strcmp(app, "stopLogprint")) service_app_exit();
	if(!strcmp(operation, APP_CONTROL_OPERATION_DEFAULT) && !strcmp(app, "logPrint"))
	{
		dlog_print(DLOG_INFO, LOG_TAG, "before exit: In app_control");

		dlog_print(DLOG_INFO, LOG_TAG, "app_id %s", app);

		app_control_remove_extra_data(app_control, "app");
		ecore_timer_add(1, logPrint, NULL);
	}
	return;
}

Modify the service_app_control() function of your service app as the above code snippet. Keep other codes(in the UI app) given in my previous reply unchanged. Package the service app with the UI app. And try the steps again. You'll get to see a continuous Hello message in Log. It'll stop the service app thus printing message in Log once you click on the Label of UI app.