Hot File

Service in android

View: 577    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: References   Fields: Other

0 point/3 review File has been tested

Service is one of the four main components of the android. Service no specialized interface to perform a task that is done under the background without interaction to an interface such as: play background music, download file, handle calculations ...

Introduction

Service is one of the four main components of the android. Service no specialized interface to perform a task that is done under the background without interaction to an interface such as: play background music, download file, handle calculations ...
The lifecycle of the service:

Service, Service trong android, thu thuat android, android tips, android co ban

Note:
When there is a context that calls the startService () to start the service desired. If the service that has not been created, it will call the onCreate (), and then call onStart () and then the service runs in the background below.
If then has a context like to start this service that service was running, only the onStart method () of the service is called.
Although the service had known how many times start only 1 instance of service and just call stopService () once to end the service.
There are 2 types to start Service:
1: use the startService method (): commonly used to execute a single action and may not return results (such as playing music)
2: use bindService (): offers a client-server form interface allows apps can "conversation" (speak, interact) with service
In this example so that you can understand how the startService () himself will present how a simple application with 2 buton to startService and stopService () {}; When the service is running, it will start a music and music playback will stop when service was stop.
Bound Service will present in the next tutorial
To make this example you do turn the following steps
Step 1: create a folder in the directory res then copy a music file on it (in this example yourself using the music file with the name hoabanglang.mp3)
You can follow the following link hoabanglang.mp3 file down
Audio Player
00:00
Use Left/Right Arrow keys to advance one second, the Up/Down arrows to advance ten seconds.
00:00
Use Up/Down Arrow keys to increase or decrease volume.

Service, Service trong android, thu thuat android, android tips, android co ban

Step 2: open the file res-> values-> String.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<resources>
 
 
 
<string name="app_name">ServiceDemo</string>
 
 <string name="action_settings">Settings</string>
 
 <string name="hello_world">Hello world!</string>
 
 <string name="StartService">Start Service</string>
 
 <string name="StopService">Stop Service</string>
 
 
 
</resources>

Step 3: open  file res->layout-> activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
 
 
 
 android:layout_width="match_parent"
 
 android:layout_height="match_parent"
 
 android:orientation="vertical"
 
 android:paddingLeft="5dp"
 
 android:paddingRight="5dp"
 
 android:paddingTop="10dp"
 
 tools:context=".MainActivity" >
 
 
 
<Button
 
 android:id="@+id/btStartService"
 
 android:layout_width="fill_parent"
 
 android:layout_height="wrap_content"
 
 android:text="@string/StartService" />
 
 
 
<Button
 
 android:id="@+id/btStopService"
 
 android:layout_width="fill_parent"
 
 android:layout_height="wrap_content"
 
 android:layout_marginTop="10dp"
 
 android:text="@string/StopService" />
 
 
 
</LinearLayout>

Step 4: create MyService.java file in the packet com.example.servicedemo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.example.servicedemo;
 
 
 
import android.app.Service;
 
import android.content.Intent;
 
import android.media.MediaPlayer;
 
import android.os.IBinder;
 
import android.util.Log;
 
 
 
public class MyService extends Service {
 
 // khai bao bien kieu MediaPlayer de quan ly file nhac
 
 MediaPlayer player;
 
 
 
@Override
 
 public IBinder onBind(Intent intent) {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onBind");
 
 return null;
 
 }
 
 
 
@Override
 
 public void onCreate() {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onCreate");
 
 // khoi tao bien player tu res
 
 player = MediaPlayer.create(this, R.raw.hoabanglang);
 
 player.setLooping(false);
 
 super.onCreate();
 
 }
 
 
 
@Override
 
 public void onStart(Intent intent, int startId) {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onStart");
 
 // chay file nhac
 
 player.start();
 
 super.onStart(intent, startId);
 
 }
 
 
 
@Override
 
 public int onStartCommand(Intent intent, int flags, int startId) {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onStartCommand");
 
 return super.onStartCommand(intent, flags, startId);
 
 }
 
 
 
@Override
 
 public void onDestroy() {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onDestroy");
 
 if (player.isPlaying()) {
 
 // neu dang choi nhac thi stop file nhac
 
 player.stop();
 
 }
 
 super.onDestroy();
 
 }
 
 
 
@Override
 
 public boolean onUnbind(Intent intent) {
 
 // TODO Auto-generated method stub
 
 Log.d("MyService", "onUnbind");
 
 return super.onUnbind(intent);
 
 }
 
 
 
}

Note: similar activity, the app wants startService must declare in AndroidManifest.xml

1
</i> <i><service android:name=".MyService"></service>

Step 5: Open MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.example.servicedemo;
 
 
 
import android.app.Activity;
 
import android.content.Intent;
 
import android.os.Bundle;
 
import android.view.Menu;
 
import android.view.View;
 
import android.view.View.OnClickListener;
 
import android.widget.Button;
 
 
 
public class MainActivity extends Activity implements OnClickListener {
 
 // khai bao Button
 
 private Button btStartService, btStopService;
 
 
 
@Override
 
 protected void onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 
 setContentView(R.layout.activity_main);
 
 
 
// link den layout de dieu khien
 
 btStartService = (Button) findViewById(R.id.btStartService);
 
 btStopService = (Button) findViewById(R.id.btStopService);
 
 
 
// dang ky lang nghe su kien onClick vao button
 
 btStartService.setOnClickListener(this);
 
 btStopService.setOnClickListener(this);
 
 }
 
 
 
@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;
 
 }
 
 
 
@Override
 
 public void onClick(View v) {
 
 // khai bao Intent de start Service tuong tu start Activity
 
 Intent mIntent = new Intent(MainActivity.this, MyService.class);
 
 switch (v.getId()) {
 
 
 
case R.id.btStartService:
 
 // click button Start Service
 
 startService(mIntent);
 
 
 
 break;
 
 case R.id.btStopService:
 
 // click button Stop Service
 
 stopService(mIntent);
 
 
 
break;
 
 
 
default:
 
 break;
 
 }
 
 
 
}
 
 
 
}

Bước 6: Mở file AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
 
 
 
 
 package="com.example.servicedemo"
 
 android:versionCode="1"
 
 android:versionName="1.0" >
 
 
 
<uses-sdk
 
 android:minSdkVersion="8"
 
 android:targetSdkVersion="17" />
 
 
 
<application
 
 android:allowBackup="true"
 
 android:icon="@drawable/ic_launcher"
 
 android:label="@string/app_name"
 
 android:theme="@style/AppTheme" >
 
 <activity
 
 android:name="com.example.servicedemo.MainActivity"
 
 android:label="@string/app_name" >
 
 <intent-filter>
 
 <action android:name="android.intent.action.MAIN" />
 
 
 
<category android:name="android.intent.category.LAUNCHER" />
 
 </intent-filter>
 
 </activity>
 
 
 
<service android:name=".MyService"></service>
 
 </application>
 
 
 
</manifest>

Note:

You can track the results of the program on the logcat and screen. . When do you for example what do not understand can leave a question on his web site will respond to you as quickly as possible. You can follow src down full link below
ServiceDemo
The results of the program

Service, Service trong android, thu thuat android, android tips, android co ban

Service, Service trong android, thu thuat android, android tips, android co ban

Service in android

Service in android Posted on 30-06-2015  Service is one of the four main components of the android. Service no specialized interface to perform a task that is done under the background without interaction to an interface such as: play background music, download file, handle calculations ... 5/10 577

Comment:

To comment you must be logged in members.

Files with category

  • Instructions for installing and configuring Windows Live Mail on Windows 10 simple

    View: 2286    Download: 0   Comment: 0   Author: none  

    Instructions for installing and configuring Windows Live Mail on Windows 10 simple

    Category: References
    Fields: Other

    0/2 review
    Windows 10 pre-installed Mail application for managing personal email on the device. The latest version of this application but improved a lot and support other email accounts such as Gmail or Yahoo Mail but users almost no "salty" really with this...

  • Step by step instructions to create social networking buttons with CSS3

    View: 1772    Download: 0   Comment: 0   Author: none  

    Step by step instructions to create social networking buttons with CSS3

    Category: References
    Fields: Other

    0.75/6 review
    CSS3 help web designers have the ability to create buttons (buttons) very nice, today I will share with you two tips decoration for button in your web becomes radiant beauty that no need to use photoshop.

  • You should understand the CSS Box Shadow

    View: 1945    Download: 0   Comment: 0   Author: none  

    You should understand the CSS Box Shadow

    Category: References
    Fields: Other

    1.3181818181818/11 review
    Effects his shadow was introduced in the previous article, however, in this article, I will guide more specific about shadow attributes, you try it with anything. First we try to learn css following paragraph:

  • How to do button movement with beautiful CSS3

    View: 1627    Download: 0   Comment: 0   Author: none  

    How to do button movement with beautiful CSS3

    Category: References
    Fields: Other

    0/3 review
    Previously to create motion effects button, you must draw the button in photoshop, then switched to flash to make it possible to move. But now you need not spend too much time doing it, all because of the emergence of CSS3.

  • Create a dropdown menu with CSS3 effects with beautiful transtions

    View: 1908    Download: 0   Comment: 0   Author: none  

    Create a dropdown menu with CSS3 effects with beautiful transtions

    Category: References
    Fields: Other

    0.45/10 review
    More of the dropdown menu you want to introduce to you, this will have the effect menu transitions (converted background of menu background from pale to dark). This menu works well on browsers like FF, Chrome, Safary, but unfortunately did not work...

  • How to hack css for all browsers you should know

    View: 1545    Download: 0   Comment: 0   Author: none  

    How to hack css for all browsers you should know

    Category: References
    Fields: Other

    1.6875/8 review
    In the process of web design, which makes it the most headaches is how to design web pages that you can run on the browser. Especially when clients use the old as IE6 browser such. However, with this article, you will easily edit interface runs well...

  • Create Accordion with extremely beautiful CSS3

    View: 1917    Download: 0   Comment: 0   Author: none  

    Create Accordion with extremely beautiful CSS3

    Category: References
    Fields: Other

    0.9/5 review
    Accordion is a technique that helps you shortened the content that you want displayed on the website. It is often used for the display of submenus. In addition you can also use it for content, images. Before applying this procedure, you have to use...

  • Create the effect Hovercho images with Webkit and CSS3 is great

    View: 1638    Download: 0   Comment: 0   Author: none  

    Create the effect Hovercho images with Webkit and CSS3 is great

    Category: References
    Fields: Other

    1.125/4 review
    WebKit is one open source application framework provides the necessary components built one for your web browser. Along with CSS3, cả have contributed to very nice effect những previously only using JavaScript or JQuery thực beige.​

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd