{"id":2432,"date":"2017-04-25T07:30:16","date_gmt":"2017-04-25T07:30:16","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2432"},"modified":"2017-04-27T15:54:12","modified_gmt":"2017-04-27T15:54:12","slug":"how-to-create-start-and-stop-service-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-create-start-and-stop-service-in-android\/","title":{"rendered":"How to create, start and stop Service in Android"},"content":{"rendered":"<p>In this tutorial how to create, start and stop Service in Android is shown.<\/p>\n<p>Android Service is a component that is used to perform operations on the background such as playing music,handle network transactions etc, which does not have any UI(User Interface).<\/p>\n<p>The service is running in the background indefinitely even if application is destroyed.<\/p>\n<p>A Service has two States:<\/p>\n<ul>\n<li>Started: A service is started when component calls startService() method. To stop the service stopService() method is called. To stop the service by self, stopSelf() method is called.<\/li>\n<li>Bound: When another component is executed or started at that time bindService() method is called. The client can unbind the service by calling unbindService().<\/li>\n<\/ul>\n<p>Methods of Service are as follows:<\/p>\n<ol>\n<li>onStartCommand(): This method is called whenever another component such as an activity request get the service started.<\/li>\n<li>onBind(): It is called when another component wants to bind with service by calling bindService().<\/li>\n<li>onUnbind(): It is called when all clients have disconnected from a particular interface.<\/li>\n<li>onRebind(): It is called when new clients have connected to the service after it had previously been notified that all had disconnected in its onUnbind() method.<\/li>\n<li>onCreate(): It is called when Service is created for the first time.<\/li>\n<li>onDestroy(): It is called whenever Service is no longer used and is being destroyed.<\/li>\n<\/ol>\n<p>The figure of Service Lifecycle is as follows:<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/webartdevelopers.com\/wp-content\/uploads\/2017\/04\/service_lifecycle.png\" width=\"389\" height=\"507\" \/><\/p>\n<p style=\"text-align: left;\">Code:<\/p>\n<p style=\"text-align: left;\">strings.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;resources&gt;<\/p>\n<p>&lt;string name=&#8221;app_name&#8221;&gt;Demo&lt;\/string&gt;<br \/>\n&lt;string name=&#8221;action_settings&#8221;&gt;Settings&lt;\/string&gt;<br \/>\n&lt;string name=&#8221;h&#8221;&gt;Program of Service&lt;\/string&gt;<br \/>\n&lt;string name=&#8221;mes1&#8243;&gt;Start Service&lt;\/string&gt;<br \/>\n&lt;string name=&#8221;mes2&#8243;&gt;Stop Service&lt;\/string&gt;<\/p>\n<p>&lt;\/resources&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@ id\/textView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;82dp&#8221;<br \/>\nandroid:text=&#8221;@string\/h&#8221; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&#8221;@ id\/button1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_below=&#8221;@ id\/textView1&#8243;<br \/>\nandroid:layout_marginTop=&#8221;117dp&#8221;<br \/>\nandroid:layout_toLeftOf=&#8221;@ id\/textView1&#8243;<br \/>\nandroid:onClick=&#8221;start&#8221;<br \/>\nandroid:text=&#8221;@string\/mes1&#8243; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&#8221;@ id\/button2&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignBottom=&#8221;@ id\/button1&#8243;<br \/>\nandroid:layout_toRightOf=&#8221;@ id\/textView1&#8243;<br \/>\nandroid:onClick=&#8221;stop&#8221;<br \/>\nandroid:text=&#8221;@string\/mes2&#8243; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.content.Intent;<br \/>\nimport android.util.Log;<br \/>\nimport android.view.Menu;<br \/>\nimport android.view.View;<br \/>\npublic class MainActivity extends Activity {<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nLog.d(&#8220;Service&#8221;,&#8221;onCreate() invoked&#8221;);<br \/>\n}<\/p>\n<p>\/\/method to start service<br \/>\npublic void start(View v)<br \/>\n{<br \/>\nstartService(new Intent(getBaseContext(),Secondjava.class));<br \/>\n}<\/p>\n<p>\/\/method to stop service<br \/>\npublic void stop(View v)<br \/>\n{<br \/>\nstopService(new Intent(getBaseContext(),Secondjava.class));<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Secondjava.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.app.Service;<br \/>\nimport android.content.Intent;<br \/>\nimport android.os.IBinder;<br \/>\nimport android.widget.Toast;<\/p>\n<p>public class Secondjava extends Service{<\/p>\n<p>@Override<br \/>\npublic IBinder onBind(Intent arg0)<br \/>\n{<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic int onStartCommand(Intent intent,int flags,int startID)<br \/>\n{<br \/>\nToast.makeText(this, &#8220;Service Started&#8221;, Toast.LENGTH_LONG).show();<br \/>\nreturn START_STICKY;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onDestroy()<br \/>\n{<br \/>\nsuper.onDestroy();<br \/>\nToast.makeText(this, &#8220;Service Stopped&#8221;, Toast.LENGTH_LONG).show();<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>AndroidManifest.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;manifest xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\npackage=&#8221;com.example.demo&#8221;<br \/>\nandroid:versionCode=&#8221;1&#8243;<br \/>\nandroid:versionName=&#8221;1.0&#8243; &gt;<\/p>\n<p>&lt;uses-sdk<br \/>\nandroid:minSdkVersion=&#8221;8&#8243;<br \/>\nandroid:targetSdkVersion=&#8221;17&#8243; \/&gt;<\/p>\n<p>&lt;application<br \/>\nandroid:allowBackup=&#8221;true&#8221;<br \/>\nandroid:icon=&#8221;@drawable\/ic_launcher&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221;<br \/>\nandroid:theme=&#8221;@style\/AppTheme&#8221; &gt;<br \/>\n&lt;activity<br \/>\nandroid:name=&#8221;com.example.demo.MainActivity&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221; &gt;<br \/>\n&lt;intent-filter&gt;<br \/>\n&lt;action android:name=&#8221;android.intent.action.MAIN&#8221; \/&gt;<\/p>\n<p>&lt;category android:name=&#8221;android.intent.category.LAUNCHER&#8221; \/&gt;<br \/>\n&lt;\/intent-filter&gt;<br \/>\n&lt;\/activity&gt;<br \/>\n&lt;service android:name=&#8221;.Secondjava&#8221;&gt;&lt;\/service&gt;<br \/>\n&lt;\/application&gt;<\/p>\n<p>&lt;\/manifest&gt;<\/p>\n<p><a href=\"https:\/\/youtu.be\/fQWxf2NoeFs\">https:\/\/youtu.be\/fQWxf2NoeFs<\/a><\/p>\n<p><a href=\"https:\/\/youtu.be\/D-9hsUhdiWc\">https:\/\/youtu.be\/D-9hsUhdiWc<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to create, start and stop Service in Android is shown. Android Service is a component that is used to perform operations on the background such as playing music,handle network transactions etc, which does not have any UI(User Interface). The service is running in the background indefinitely even if application is destroyed. A Service has two States: Started: A service is started when component calls startService() method. To stop the service stopService() method is called. To stop the service by self, stopSelf() method is called. Bound: When another component is executed or started at that time bindService() method is called. The client can unbind the service by calling unbindService(). Methods of Service are as follows: onStartCommand(): This <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[72,73],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2432"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2432"}],"version-history":[{"count":3,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2432\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2432\/revisions\/2436"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}