android Service的基本使用方法

Service的基本概念呢,在这里就不阐述了。我们直接说,Service是如何使用的吧。

 我们以一个音乐播放的例子讲述:

 activity_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" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="startMyService"
        android:text="启动服务" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="stopMyService"
        android:text="停止服务" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="bindMyService"
        android:text="绑定服务" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="unBindMyService"
        android:text="解绑服务" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="playMusic"
        android:text="播放" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="pauseMusic"
        android:text="暂停" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="continueMusic"
        android:text="继续" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="stopMusic"
        android:text="停止" />

</LinearLayout>

MainActivity.java


public class MyServiceActivity extends Activity {

	private ServiceConnection conn ;
	private Intent mService;
	private MyDefineService mdService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mService = new Intent(this,MyDefineService.class);

		conn = new ServiceConnection() {

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				MyDefineService.MyBindService s = (MyDefineService.MyBindService)service;
				mdService = s.getService();
				Log.i("INFO","mdService============="+mdService);
			}

			@Override
			public void onServiceDisconnected(ComponentName name) {
				
			}
		};

	}

	public void startMyService(View v){
		this.startService(mService);//intent激活组件
	}

	public void stopMyService(View v){

		this.stopService(mService);
	}

	public void bindMyService(View v){
		this.bindService(mService, conn, Context.BIND_AUTO_CREATE);
	}

	public void unBindMyService(View v){
		this.unbindService(conn);
	}

	public void playMusic(View v){
		mdService.playMusic();
	}

	public void pauseMusic(View v){
		mdService.pauseMusic();
	}

	public void continueMusic(View v){
		mdService.continueMusic();
	}

	public void stopMusic(View v){
		mdService.stopMusic();
	}

}

MyDefineService:

public class MyDefineService extends Service {

	private MediaPlayer player = new MediaPlayer();
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Log.i("INFO","启动服务。。。");
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("INFO","销毁服务。。。");
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		Log.i("INFO","绑定onBind");
		return new MyBindService();
	}
	
	class MyBindService extends Binder{
		public MyDefineService getService(){
			return MyDefineService.this;
		}
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i("INFO","解除绑定onUnbind");
		return super.onUnbind(intent);
	}
	
	public void playMusic(){
		try {
			player.reset();
			player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath()+"/baorong.mp3");
			player.prepare();
			player.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void pauseMusic(){
		player.pause();
	}

	public void continueMusic(){
		player.start();
	}

	public void stopMusic(){
		player.stop();
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值