{"id":4592,"date":"2016-11-22T04:00:18","date_gmt":"2016-11-22T04:00:18","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=4592"},"modified":"2022-01-12T10:27:38","modified_gmt":"2022-01-12T10:27:38","slug":"android-development-sample","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/android-development-sample\/","title":{"rendered":"Android Development Sample"},"content":{"rendered":"<p>Hello! Today I want to show you how to create, start and stop simple service in Android OS.<\/p>\n<p>Thus, the service is a certain task that runs in the background and does not use UI. The service can be started and stopped from applications and other services. Also, you can connect to the running service and interact with it.<!--more--><\/p>\n<p>As an example, let\u2019s consider the algorithm of a mailer. It consists of applications and services. The service works in the background and periodically checks for new mail, downloads it and displays a notification. When you launch the app, it shows you those emails downloaded by the service. Also, the application can connect to the service, and change, for example, the frequency of email scan or close the service, if the constant checking is no longer needed.<!--more--><\/p>\n<p>So the service is needed to make your task working, even when the app is closed. In this example, you will understand what methods of cooperation between the application and service exist. We will create a simple service that will print anything to the log. And the app will start and stop the service.<\/p>\n<p>Create a project:<\/p>\n<p><strong>Project name<\/strong>: P0921_ServiceSimple<br \/>\n<strong>Build Target<\/strong>: Android 2.3.3<br \/>\n<strong>Application name<\/strong>: ServiceSimple<br \/>\n<strong>Package name<\/strong>: ru.startandroid.develop.p0921servicesimple<br \/>\n<strong>Create Activity<\/strong>: MainActivity<br \/>\nAdd such text to <strong>strings.xml<\/strong>:<\/p>\n<p>[code language=&#8221;xml&#8221;]<br \/>\n&lt;string name=&quot;start&quot;&gt;Start service&lt;\/string&gt;<br \/>\n&lt;string name=&quot;stop&quot;&gt;Stop service&lt;\/string&gt;<br \/>\n[\/code]<\/p>\n<p>And such text to <strong>main.xml<\/strong>:<\/p>\n<p>[code language=&#8221;xml&#8221;]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:orientation=&quot;vertical&quot;&gt;<br \/>\n\t&lt;Button android:id=&quot;@+id\/btnStart&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:onClick=&quot;onClickStart&quot; android:text=&quot;@string\/start&quot;&gt;<br \/>\n\t&lt;\/Button&gt;<br \/>\n\t&lt;Button android:id=&quot;@+id\/btnStop&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:onClick=&quot;onClickStop&quot; android:text=&quot;@string\/stop&quot;&gt;<br \/>\n\t&lt;\/Button&gt;<br \/>\n\t&lt;ProgressBar android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:indeterminate=&quot;true&quot;&gt;<br \/>\n\t&lt;\/ProgressBar&gt;<br \/>\n&lt;\/LinearLayout&gt;<br \/>\n[\/code]<\/p>\n<p>Two buttons \u2013 to start and stop the service, and the ProgressBar.<\/p>\n<p>Create <strong>MyService<\/strong> class inherited from the <strong>android.app.Service<\/strong> near the <strong>MainActivity<\/strong>:<\/p>\n<p style=\"max-width: 500px; margin: 0 auto;\"><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4594\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-1.png\" alt=\"android-development-sample-1\" width=\"100%\" height=\"auto\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-1.png 528w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-1-253x300.png 253w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/><\/a><\/p>\n<p><strong>MyService.java:<\/strong><\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\npackage ru.startandroid.develop.p0921servicesimple;<\/p>\n<p>import android.app.Service;<br \/>\nimport android.content.Intent;<br \/>\nimport android.os.IBinder;<br \/>\nimport android.util.Log;<\/p>\n<p>public class MyService extends Service {<\/p>\n<p>\tfinal String LOG_TAG = &quot;myLogs&quot;;<\/p>\n<p>\tpublic void onCreate() {<br \/>\n\t\tsuper.onCreate();<br \/>\n\t\tLog.d(LOG_TAG, &quot;onCreate&quot;);<br \/>\n\t}<\/p>\n<p>\tpublic int onStartCommand(Intent intent, int flags, int startId) {<br \/>\n\t\tLog.d(LOG_TAG, &quot;onStartCommand&quot;);<br \/>\n\t\tsomeTask();<br \/>\n\t\treturn super.onStartCommand(intent, flags, startId);<br \/>\n\t}<\/p>\n<p>\tpublic void onDestroy() {<br \/>\n\t\tsuper.onDestroy();<br \/>\n\t\tLog.d(LOG_TAG, &quot;onDestroy&quot;);<br \/>\n\t}<\/p>\n<p>\tpublic IBinder onBind(Intent intent) {<br \/>\n\t\tLog.d(LOG_TAG, &quot;onBind&quot;);<br \/>\n\t\treturn null;<br \/>\n\t}<\/p>\n<p>\tvoid someTask() {<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>The service has <strong>onCreate<\/strong> and <strong>onDestroy<\/strong> methods that run in case of starting and stopping the service.<\/p>\n<p><strong>OnStartCommand<\/strong> method triggers when the service starts with <strong>startService<\/strong> method. There we run our method <strong>someTask<\/strong>, which is empty for now. <strong>OnStartCommand<\/strong> has inbound and outbound parameters which are not used for now.<\/p>\n<p>We will code the work of the service in<strong> someTask<\/strong> method.<\/p>\n<p>We should note the service in the manifest:<\/p>\n<p style=\"max-width: 600px; margin: 0 auto;\"><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-2.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4598\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-2.png\" alt=\"android-development-sample-2\" width=\"100%\" height=\"auto\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-2.png 865w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-2-300x90.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-2-768x231.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/a><\/p>\n<p><strong>MainActivity.java<\/strong><\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\npackage ru.startandroid.develop.p0921servicesimple;<\/p>\n<p>import android.app.Activity;<br \/>\nimport android.content.Intent;<br \/>\nimport android.os.Bundle;<br \/>\nimport android.view.View;<\/p>\n<p>public class MainActivity extends Activity {<\/p>\n<p>\tfinal String LOG_TAG = &quot;myLogs&quot;;<\/p>\n<p>\t\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\t\tsetContentView(R.layout.main);<\/p>\n<p>\t\t}<\/p>\n<p>\t\t void onClickStart(View v) {<br \/>\n\t\t\tstartService(new Intent(this, MyService.class));<br \/>\n\t\t}<\/p>\n<p>\t\tpublic void onClickStop(View v) {<br \/>\n\t\t\tstopService(new Intent(this, MyService.class));<br \/>\n\t\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Here we have two methods that trigger on the <strong>Start<\/strong> service and<strong> Stop service<\/strong> buttons. Respectively in them, we start and stop the service using <strong>startService<\/strong> and <strong>stopService<\/strong> methods. Inbound we pass Intent, that points to the service.<\/p>\n<p>So let\u2019s save everything and run the application:<\/p>\n<p style=\"max-width: 500px; margin: 0 auto;\"><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-3.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4604\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-3.png\" alt=\"android-development-sample-3\" width=\"100%\" height=\"auto\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-3.png 788w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-3-300x214.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-3-768x549.png 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/a><\/p>\n<p>Push <strong>Start service<\/strong> and check the log:<\/p>\n<p><strong>onStartCommand <\/strong><\/p>\n<p>The service was created, <strong>onCreate<\/strong> and <strong>onStartCommand<\/strong> methods triggered.<\/p>\n<p>Push <strong>Start service<\/strong> one more time:<\/p>\n<p><span style=\"color: #808080;\"><em>onStartCommand\u00a0<\/em><\/span><\/p>\n<p>The service is already created so \u00a0<strong>onCreate<\/strong> didn\u2019t trigger, but <strong>onStartCommand<\/strong> did.<\/p>\n<p>Push <strong>Stop service<\/strong>:<\/p>\n<p>Service was destroyed.<\/p>\n<p>Let\u2019s make sure that service doesn\u2019t depend on the application. Push <strong>Start service<\/strong><\/p>\n<p><span style=\"color: #808080;\"><em>onCreate<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>onStartCommand<\/em> <\/span><\/p>\n<p>Service have started. Close the application pushing the Back button. Logs are empty, <strong>onDestroy<\/strong> method didn\u2019t trigger, service is still working.<br \/>\nLong push the Home button to see the recent applications:<\/p>\n<p style=\"max-width: 500px; margin: 0 auto;\"><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-4.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4606\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-4.png\" alt=\"android-development-sample-4\" width=\"100%\" height=\"auto\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-4.png 788w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-4-300x214.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/android-development-sample-4-768x549.png 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/a><\/p>\n<p>Open our app again and push <strong>Stop service<\/strong>. In logs we see:<\/p>\n<p><strong>onDestroy<\/strong><\/p>\n<p>The service is destroyed.<\/p>\n<p>Now let\u2019s try to do something meaningful in <strong>onStartCommand<\/strong> method. Rewrite the <strong>someTask<\/strong> method:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nvoid someTask() {<br \/>\n\tfor (int i = 1; i&amp;lt;=5; i++) {<br \/>\n\t\tLog.d(LOG_TAG, &quot;i = &quot; + i);<br \/>\n\t\ttry {<br \/>\n\t\t\tTimeUnit.SECONDS.sleep(1);<br \/>\n\t\t} catch (InterruptedException e) {<br \/>\n\t\t\te.printStackTrace();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Let\u2019s output the notes from the service to the log each second:<\/p>\n<p><span style=\"color: #808080;\"><em>onCreate<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> onStartCommand<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> i = 1<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> i = 2<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> i = 3<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> i = 4<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em> i = 5<\/em><\/span><\/p>\n<p>So the service is running in the main thread.<\/p>\n<p>Let\u2019s expose the cycle with pauses to the separate thread and also run the <strong>stopSelf<\/strong> method. Rewrite the <strong>someTask<\/strong> method:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nvoid someTask() {<br \/>\n\tnew Thread(new Runnable() {<br \/>\n\t\tpublic void run() {<br \/>\n\t\t\tfor (int i = 1; i&lt;=5; i++) {<br \/>\n\t\t\tLog.d(LOG_TAG, &quot;i = &quot; + i);<br \/>\n\t\ttry {<br \/>\n\t\t\tTimeUnit.SECONDS.sleep(1);<br \/>\n\t\t} catch (InterruptedException e) {<br \/>\n\t\t\te.printStackTrace();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tstopSelf();<br \/>\n     }<br \/>\n   }).start();<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>We have exposed the code to the separate thread and added the <strong>stopSelf<\/strong> method which is similar to <strong>stopService<\/strong> method. It stops the service that called it.<br \/>\nSave everything, execute and push Start service. <strong>ProgressBar<\/strong> is spinning, screen is not blocked, logs are coming:<\/p>\n<p><span style=\"color: #808080;\"><em>onCreate<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>onStartCommand<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>i = 1<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>i = 2<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>i = 3<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>i = 4<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>i = 5<\/em><\/span><br \/>\n<span style=\"color: #808080;\"><em>onDestroy<\/em> <\/span><\/p>\n<p>So the service was created, done its job and was stopped by the <strong>stopSelf<\/strong> method.<\/p>\n<blockquote><p>In case you missed, we posted once more <a href=\"https:\/\/assignmentshark.com\/blog\/sample-android-application-code\/\" target=\"_blank\" rel=\"noopener noreferrer\">sample Android application code<\/a> before<\/p>\n<p><em>We recommend viewing this Android development sample at the beginning, and during, your course of computer science so that you have an idea what your professor expects you to hand in. The sample of assignment is perfect for most students and contains material that can help you to deal with your own homework. If you have any question about your assignment, you can always get <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">online assignment help<\/a> from AssignmentShark.com. We have a team of experts that includes only professionals with long-term experience in dealing with any assignment. All assignments are done quickly and according to your requirements. Use our service to make your homework done successfully.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today I want to show you how to create, start and stop simple service in Android OS. Thus, the service is a certain task that runs in the background and does not use UI. The service can be started and stopped from applications and other services. Also, you can connect to the running service [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-4592","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=4592"}],"version-history":[{"count":15,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4592\/revisions"}],"predecessor-version":[{"id":13009,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4592\/revisions\/13009"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=4592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=4592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=4592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}