A blog or place for Mobile Application Developers to improve his skills and learn new .

Thursday, 19 December 2013

How to start and stop service in android.

Here we see how to start and stop a service dynamically.

What is Service ?

A service is a
component which runs in the background without direct interaction with the
user. As the service has no user interface, it is not bound to the lifecycle of
an activity.

As there are two methods
by which we start and stop service :

startService (Intent service)

stopService (Intent service)

In order to start a service
we need to call a method name onStartCommand(Intent
intent, int flags, int startId)and to stop a service call a method name
onDestroy() . All this work we do in
class which extends Service class.

Create a project with
the following details :

·ProjectName: StartStopServicee

·PackageName: sat.tuts4mobile.startstop

·ActivityName: MainActivity

In the MainActivity.java file, copy the following code :

package
sat.tuts4mobile.startstop;

import
android.os.Bundle;

import
android.app.Activity;

import
android.content.Intent;

import
android.view.Menu;

import
android.view.View;

import
android.widget.Button;

public class
MainActivity extends Activity {

@Override

protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button start = (Button)
findViewById(R.id.button1);

start.setOnClickListener(new
View.OnClickListener() {

@Override

public void
onClick(View v) {

//
TODO Auto-generated method stub

startService(new
Intent(MainActivity.this, MyService.class));

}

});

Button stop = (Button)
findViewById(R.id.button2);

stop.setOnClickListener(new
View.OnClickListener() {

@Override

public void
onClick(View v) {

//
TODO Auto-generated method stub

stopService(new
Intent(MainActivity.this, MyService.class));

}

});

}

@Override

public boolean
onCreateOptionsMenu(Menu menu) {

// Inflate the menu;
this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main,
menu);

return true;

}

}

Create a new class
name MyService.java file, copy the
following code :

Portable provisions can't simply just fun you yet even it can supporting your organization prerequisites by permitting you join with your work at whatever time, anyplace. Abroad versatile database joining results in going with a portable computer incorporate the era, supply, taking care of, and enlargement of requisitions. Cell program creators, outline provisions for web fueled portable mechanism, which incorporate Cellular Devices, Individual electronic staff and Business electronic staff. See more at:.....iPhone application development // Andriod application development // mobile application development

I like ipad is a multipurpose mechanism. It is for stimulation as well as it has functionalities to serve as a helpful unit for business. You can complete numerous operations of business with ipad. It has all functionalities like iphone aside from you can not make a call. Today we will see some functional requisitions for business.Android application development // Android application development // mobile app developers

I have been searching for various blog for getting knowledge in mobile apps development. This blog is very easy to understand. Thanks for sharing this blog.href="http://www.balladonia.com"moblile application application

Excellent information, Great post here. I found that you shared a complete information on Android Apps Development. OnGraph Technologies provide Android App development services and other Mobile app development services.

Really Great Information, Thanks for the post you have provided step by step process so anyone can understand. If are you looking best Mobile application development company in india. For understanding Mobile Application Development and Android Application Development.