 |
|
|  |
¾Èµå·ÎÀ̵å: ¼ºñ½º Service ¿¹Á¦ |
|
|
 |
8³â Àü |
¾×ƼºñƼµî »ç¿ëÀÚ ÀÎÅÍÆäÀ̽º°¡ ¾ø¾îµµ ¹é±×¶ó¿îµå¿¡¼ ½ÇÇàµÇ´Â µ¿ÀÛÀÌ ÇÊ¿äÇÒ ¶§°¡ ÀÖ½À´Ï´Ù. ¿¹¸¦ µé¸é ¹è°æÀ½¾ÇÀ̶óµçÁö ¾Èµå·ÎÀÌµå »ç¿ë·® ¸ð´ÏÅ͸µÀ̶óµçÁö, ÁÖ±âÀûÀ¸·Î ƯÁ¤ À¥»çÀÌÆ®¿¡¼ µ¥ÀÌÅ͸¦ ÀÐ¾î ¿Â´ÙµçÁö... ÀÌ·¯ÇÑ ±â´ÉÀ» ¾Èµå·ÎÀ̵忡¼´Â Service (¼ºñ½º) ¸¦ ÅëÇؼ Á¦°øÇÕ´Ï´Ù.
Âü°í: ¾Èµå·ÎÀ̵å 4´ë ÄÄÆ÷³ÍÆ®
1. ¾×ƼºñƼ (Activity)
2. ¼ºñ½º (Service)
3. ¹æ¼Û ¼ö½ÅÀÚ (Broadcast Receiver)
4. ÄÜÅÙÃ÷ Á¦°øÀÚ (Contents Provider)
°®°í °è½Å ¾Èµå·ÎÀ̵å Æù¿¡¼ ½ÇÇàµÇ´Â ¼ºñ½º¸¦ È®ÀÎÇغ¼¼ö ÀÖ½À´Ï´Ù.
[ȯ°æ¼³Á¤] - [¾ÖÇø®ÄÉÀÌ¼Ç °ü¸®ÀÚ] ¿¡¼ [½ÇÇàÁß] À» ¼±ÅÃÇϸé ÇöÀç ½ÇÇàÁßÀÎ ¼ºñ½ºµéÀ» º¼¼ö ÀÖ½À´Ï´Ù. »ý°¢º¸´Ù ¸¹Àº ¼ºñ½ºµéÀÌ ½ÇÇàÁßÀÎ °ÍÀ» ¾Ë¼ö ÀÖ½À´Ï´Ù.
À̹ø¿¹Á¦¿¡¼´Â ¾Èµå·ÎÀ̵å 4´ë ÄÄÆ÷³ÍÆ®Áß ÇϳªÀÎ Service ¸¦ ±¸ÇöÇØ º¸°Ú½À´Ï´Ù.
±âº»ÀûÀÎ ¼ø¼´Â ´ÙÀ½°ú °°½À´Ï´Ù.
1. »ç¿ëÇÒ Service (*.java)¸¦ ¸¸µç´Ù
2. AndroidManifest.xml ¿¡ Service¸¦ µî·ÏÇÑ´Ù
3. Service ¸¦ ½ÃÀÛÇϵµ·Ï È£ÃâÇÑ´Ù
½ÇÇàÇϸé [¼ºñ½º ½ÃÀÛÇϱâ] ¹öÆ°À» ´©¸£¸é ¹è°æÀ½¾ÇÀ» Àç»ýÇÏ´Â ¼ºñ½º°¡ ½ÃÀ۵ǰí [¼ºñ½º Á¾·áÇϱâ] ¹öÆ°À» ´©¸£¸é Á¾·á°¡ µË´Ï´Ù.
1. »ç¿ëÇÒ Service (*.java)¸¦ ¸¸µé±â
[MyService.java] ÀÛ¼º
- Service ¸¦ »ó¼Ó
¿©±â¼ ±¸ÇöÇØ¾ß ÇÏ´Â Áß¿ä ¸Þ¼Òµå´Â
onStartCommand() : ´Ù¸¥ ÄÄÆ÷³ÍÆ®¿¡¼ startService()¸¦ È£ÃâÇÏ¿©¼ ¼ºñ½º°¡ ½ÃÀ۵Ǹé ÀÌ ¸Þ¼Òµå°¡ È£ÃâµË´Ï´Ù. ÀÛ¾÷ÀÌ ¿Ï·áµÇ¾úÀ» °æ¿ì stopSelf() ³ª stopService() ¸¦ È£ÃâÇÏ¿© ¼ºñ½º¸¦ Á¾·áÇÏ¿©¾ß ÇÕ´Ï´Ù. ±×·¸Áö ¾ÊÀ¸¸é ¹«ÇÑ ½ÇÇàµË´Ï´Ù.
// ¼ºñ½º Ŭ·¡½º¸¦ ±¸ÇöÇÏ·Á¸é, Service ¸¦ »ó¼Ó¹Þ´Â´Ù
public class MyService extends Service {
MediaPlayer mp; // À½¾Ç Àç»ýÀ» À§ÇÑ °´Ã¼
@Override
public IBinder onBind(Intent intent) {
// Service °´Ã¼¿Í (ȸé´Ü Activity »çÀÌ¿¡¼)
// Åë½Å(µ¥ÀÌÅ͸¦ ÁÖ°í¹ÞÀ») ¶§ »ç¿ëÇÏ´Â ¸Þ¼µå
// µ¥ÀÌÅ͸¦ Àü´ÞÇÒ ÇÊ¿ä°¡ ¾øÀ¸¸é return null;
return null;
}
@Override
public void onCreate() {
super.onCreate();
// ¼ºñ½º¿¡¼ °¡Àå ¸ÕÀú È£ÃâµÊ(ÃÖÃÊ¿¡ Çѹø¸¸)
Log.d("test", "¼ºñ½ºÀÇ onCreate");
mp = MediaPlayer.create(this, R.raw.chacha);
mp.setLooping(false); // ¹Ýº¹Àç»ý
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// ¼ºñ½º°¡ È£ÃâµÉ ¶§¸¶´Ù ½ÇÇà
Log.d("test", "¼ºñ½ºÀÇ onStartCommand");
mp.start(); // ³ë·¡ ½ÃÀÛ
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
// ¼ºñ½º°¡ Á¾·áµÉ ¶§ ½ÇÇà
mp.stop(); // À½¾Ç Á¾·á
Log.d("test", "¼ºñ½ºÀÇ onDestroy");
}
}
2. AndroidManifest.xml ¿¡ Service¸¦ µî·ÏÇÑ´Ù
<application> ¾È¿¡ ³Ö¾îÁÖ°í name ÇÁ·¯ÆÛƼ´Â Ŭ·¡½º À̸§À» µî·ÏÇÕ´Ï´Ù
[AndroidManifest.xml] ÀÛ¼º
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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>
3. Service ¸¦ ½ÃÀÛÇϵµ·Ï È£ÃâÇÑ´Ù
[¾×ƼºñƼ] ÀÛ¼º
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:text="¼ºñ½º"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:text="¼ºñ½º ½ÃÀÛÇϱâ" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:text="¼ºñ½º Á¾·áÇϱâ" />
</RelativeLayout>
[MainActivity] ÀÛ¼º
ÀÎÅÙÆ®¸¦ »ç¿ëÇÏ¿© ¼ºñ½º¸¦ °¡µ¿ / Áß´ÜÇÕ´Ï´Ù.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ¼ºñ½º Service - ¾Èµå·ÎÀ̵åÀÇ 4´ë ÄÄÆ÷³ÍÆ® Áß Çϳª
// ȸéÀÌ ¾øÀÌ µ¿ÀÛÇÔ
// º¸Åë Activity ¿¡¼ È£ÃâµÇ¾î ½ÃÀÛÇÔ
// 1. »ç¿ëÇÒ Service (*.java)¸¦ ¸¸µç´Ù
// 2. AndroidManifest.xml ¿¡ Service¸¦ µî·ÏÇÑ´Ù
// 3. Service ¸¦ ½ÃÀÛÇϵµ·Ï È£ÃâÇÑ´Ù
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// ¼ºñ½º ½ÃÀÛÇϱâ
Log.d("test", "¾×ƼºñƼ-¼ºñ½º ½ÃÀÛ¹öưŬ¸¯");
Intent intent = new Intent(
getApplicationContext(),//ÇöÀçÁ¦¾î±ÇÀÚ
MyService.class); // À̵¿ÇÒ ÄÄÆ÷³ÍÆ®
startService(intent); // ¼ºñ½º ½ÃÀÛ
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// ¼ºñ½º Á¾·áÇϱâ
Log.d("test", "¾×ƼºñƼ-¼ºñ½º Á¾·á¹öưŬ¸¯");
Intent intent = new Intent(
getApplicationContext(),//ÇöÀçÁ¦¾î±ÇÀÚ
MyService.class); // À̵¿ÇÒ ÄÄÆ÷³ÍÆ®
stopService(intent); // ¼ºñ½º Á¾·á
}
});
} // end of onCreate
} // end of class |
|
̵̧ : 368 |
̵̧
¸ñ·Ï
|
|
|  |
|