1#
java文件的代码

项目demo可去安卓开发qq群273843464下载
public class Activity01 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button_start = (Button)findViewById(R.id.start);
        Button button_stop = (Button)findViewById(R.id.stop);
        button_start.setOnClickListener(start);
        button_stop.setOnClickListener(stop);
        //startService(new Intent("com.yarin.Android.MUSIC"));

    }
    

    private OnClickListener start = new OnClickListener()
    {
        public void onClick(View v)
        {  

            startService(new Intent("com.yarin.Android.MUSIC"));
        }
    };
   //ֹͣ��ť
    private OnClickListener stop = new OnClickListener()
    {
        public void onClick(View v)
        {
            //ֹͣService
            stopService(new Intent("com.yarin.Android.MUSIC"));      
        }
    };

}


Service代码:

public class MusicService extends Service
{

    private MediaPlayer    player;

    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);

        player = MediaPlayer.create(this, R.raw.test);

        player.start();
    }

    public void onDestroy()
    {
        super.onDestroy();

        player.stop();
    }

}
sdsdsds

然后是样式


    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
        android:id="@+id/start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="开始播放"/>
        android:id="@+id/stop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="停止播放"  
    />

最后是manifest的注册

                  android:label="@string/app_name">