main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sel" 
        android:layout_gravity="center"/>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stop" />

</LinearLayout>
sel.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 눌러졌을때 보여질 이미지 선택 -->
    <item android:state_pressed="true"
          android:drawable="@drawable/red"/>
	
    <!-- 기본 상태에서 보여질 이미지 -->
	<item android:drawable="@drawable/yellow"/>
	    
</selector>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gusfree.service"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ServiceActivity"
            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="MusicService"></service>
    </application>

</manifest>





ServiceActivity.java
package com.gusfree.service;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class ServiceActivity extends Activity {
    ImageButton imageButton;
    Button button;
    MediaPlayer mp;
    Intent intent; 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageButton=(ImageButton) findViewById(R.id.imageButton1);
        button = (Button)findViewById(R.id.button1);
       
        ClickListener listener = new ClickListener();
        imageButton.setOnClickListener(listener);
        button .setOnClickListener(listener) ;                
        
        intent =new Intent();
        intent.setClass(this, MusicService.class);
        intent.putExtra("vol", 100);
        
        //mp = MediaPlayer.create(this, R.raw.riverside);
        
        
    }
    //inner class
    class ClickListener implements android.view.View.OnClickListener{
  
		@Override
		public void onClick(View v) {
			
			if(v==imageButton){//음악 시작
				imageButton.setEnabled(false);//못누르게

				//onStart메서드를 호출한다.
				startService(intent);
				
				//mp.start();
			}else if(v==button) {//음악 종료
				imageButton.setEnabled(true);
				
				//onDestory 메서드를 호출한다.
				stopService(intent);
				
				//mp.pause();
			}
		}
    }
}
MusicService.java
package com.gusfree.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MusicService extends Service{

	MediaPlayer mp;
	
	//시작될때 호출
	@Override
	public void onCreate() {		
		super.onCreate();
		//에러 체크
		mp=MediaPlayer.create(getApplicationContext(), R.raw.riverside);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		int vol =intent.getIntExtra("vol",50);
		mp.setVolume(vol, vol);
		mp.start();
		super.onStart(intent, startId);
	}
	
	// 종료될때 호출
	@Override
	public void onDestroy() {		
		super.onDestroy();
		mp.stop();
		mp.release();//메모리에서 mp3제거
		
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// Activity 와 Service를 묶는 기능..
		return null;
	}
}

음악을 실행 시키고 서비스창에 갑







'Android > 2012.04월 강좌' 카테고리의 다른 글

8일차 GoogleMap  (0) 2012.05.07
8일차 Google Key 인증  (0) 2012.05.07
8일차 Service (음악 파일 재생)  (0) 2012.05.07
8일차 숙제  (0) 2012.05.07
8일차 FileIO 3 - 저장된 파일 가져오기  (0) 2012.05.07

+ Recent posts