android service组件学习-1

这里使用播放器播放音乐来做例子.直接上代码:


package org.example.service;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MServices extends Service {
	
	//使用该类来播放音乐
	private MediaPlayer player;
	
	/**
	 * DOC 该方法在生命周期中只会调用一次
	 */
	@Override
	public void onCreate() {
		if (player == null) {
			//实例化播放器
			player=new MediaPlayer();
			try {
				//设置指向的歌曲
				player.setDataSource("/sdcard/1.mp3");
				player.prepare();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		super.onCreate();
	}

	/**
	 * DOC 该方法在生命周期中会被多次掉的用
	 * (使用activity.startService()方法)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Bundle extras = intent.getExtras();
		if (extras != null) {
			//根据不同的请求来处理动作
			int data = extras.getInt("data");
			switch (data) {
			case 1:
				play();
				break;
			case 2:
				pause();
				break;
			case 3:
				stop();
				break;

			default:
				break;
			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	private void stop() {
		if (player != null) {
			System.out.println("stop");
			player.stop();
			try {
				player.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void pause() {
		if (player != null && player.isPlaying()) {
			System.out.println("pause");
			player.pause();
		}
	}

	private void play() {
		if (player != null && !player.isPlaying()) {
			System.out.println("play");
			player.start();
		}
	}

	@Override
	public void onDestroy() {
		if (player != null) {
			player.stop();
			player.release();
		}
		super.onDestroy();
	}

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

}

package org.example.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServicesActivity extends Activity implements OnClickListener {

	private Button play, pause, stop, close, exit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		play = (Button) findViewById(R.id.play);
		pause = (Button) findViewById(R.id.pause);
		stop = (Button) findViewById(R.id.stop);
		close = (Button) findViewById(R.id.close);
		exit = (Button) findViewById(R.id.exit);

		play.setOnClickListener(this);
		pause.setOnClickListener(this);
		stop.setOnClickListener(this);
		close.setOnClickListener(this);
		exit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		//启用service
		Intent intent = new Intent(this, MServices.class);
		int i = 0;
		if (v.getId() == play.getId()) {
			i = 1;
		} else if (v.getId() == pause.getId()) {
			i = 2;
		} else if (v.getId() == stop.getId()) {
			i = 3;
		} else if (v.getId() == close.getId()) {
			stopService(intent);
		} else if (v.getId() == exit.getId()) {
			this.finish();
		}
		if (i != 0) {
			intent.putExtra("data", i);
			startService(intent);
		}
	}
}

<?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" >

    <Button
        android:id="@+id/play"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="play" />

    <Button
        android:id="@+id/pause"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="pause" />

    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="stop" />

    <Button
        android:id="@+id/close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="close" />

    <Button
        android:id="@+id/exit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="exit" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.service"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ServicesActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MServices"></service>
    </application>

</manifest>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值