效果图:
layout的main.xml:
MainActivity:
package com.wyl.servicedemo;import com.wyl.servicedemo.MyBindService.MyBinder;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { Intent intent ; Intent intent2; MyBindService service;//定义一个类型为继承了Service类的MyBindService类的成员变量, /* * 使用bindService(intent2, conn, Service.BIND_AUTO_CREATE);方式开启一个 * Service服务必须实例化一个ServiceConnection用来接收extends Service的MyBindService里 * 回传的数据 */ ServiceConnection conn = new ServiceConnection() { /** * 当启动源跟Service的连接意外丢失的时候会调用这个方法 * 比如当Service崩溃了或者被强行kill了。 */ @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub int s = service.SIZE;// Toast.makeText(this, "onServiceConnected()方法所在线程为:"+Thread.currentThread().getName(), 100).show(); System.out.println("SIZE:"+s+",onServiceDisconnected()方法所在线程为:"+Thread.currentThread().getName()); } @Override public void onServiceConnected(ComponentName arg0, IBinder binder) { //接收会传来的数据,根据这个service我们可以获取一些数据 service = ((MyBinder)binder).getService(); int s = service.SIZE;// Toast.makeText(this, "onServiceConnected()方法所在线程为:"+Thread.currentThread().getName(), 100).show(); System.out.println("SIZE:"+s+",onServiceConnected()方法所在线程为:"+Thread.currentThread().getName()); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { switch (view.getId()) { case R.id.start: intent = new Intent(MainActivity.this,MyService.class); System.out.println("onClick.startService()"); Toast.makeText(this, "开启线程startService", 500).show(); startService(intent); break; case R.id.stop: System.out.println("onClick.stopService()"); Toast.makeText(this, "关闭线程stopService", 500).show(); stopService(intent); break; case R.id.bind://绑定 intent2 = new Intent(MainActivity.this,MyBindService.class); //第三个参数是自动开启服务的作用,第二个参数不能够为空,且为ServiceConnection conn类型, bindService(intent2, conn, Service.BIND_AUTO_CREATE); System.out.println("onClick.bindService()"); Toast.makeText(this, "开启绑定", 500).show(); break; case R.id.unbind://解除绑定 stopService(intent2); unbindService(conn);//解除绑定,这个参数一个不能够为空,unbindService(ServiceConnection conn); System.out.println("onClick.unbindService()"); Toast.makeText(this, "解除绑定", 500).show(); break; case R.id.music://播放音乐 service.Play(); Toast.makeText(this, "播放音乐", 500).show(); break; case R.id.stopmusic://暂停音乐 service.Play(); Toast.makeText(this, "暂停音乐", 500).show(); break; } }}
MyService.java (这个service只是用来普通的stopService(),和startService()):
package com.wyl.servicedemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service{ @Override /* * Parameters intent The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here. Returns Return an IBinder through which clients can call on to the service. */ public void onCreate() { System.out.println("BindService.onCreate()"); super.onCreate(); }; public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("BindService.onBind"); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("onStartCommand()方法。。。。"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("myServie.ondestroy()...."); super.onDestroy(); }}
MyBindService.java :用bind的方式来绑定service,
package com.wyl.servicedemo;import android.app.Service;import android.content.Intent;import android.content.ServiceConnection;import android.os.Binder;import android.os.IBinder;public class MyBindService extends Service{ public static int SIZE = 3; @Override public void onCreate() { System.out.println("onCreate()方法。。。"); super.onCreate(); } /** * bind方式开启service,必须写一个类继承Binder, * 然后再IBinder onBind(Intent arg0)方法中返回所需要返回的值 * @author wyl * */ public class MyBinder extends Binder{ public MyBindService getService(){ System.out.println("MyBinder extends Binder的MyBindService getService()方法。。。"); return MyBindService.this; } } @Override public IBinder onBind(Intent arg0) { System.out.println("public IBinder onBind(Intent arg0) 方法。。。"); /* * onBind(Intent arg0),想回传数据, * 必须写上面的public class MyBinder extends Binder */ return new MyBinder(); } @Override public boolean onUnbind(Intent intent) { System.out.println("onUnbind(Intent intent)方法。。。"); return super.onUnbind(intent); } @Override public void unbindService(ServiceConnection conn) { System.out.println("unbindService(ServiceConnection conn)方法。。。"); super.unbindService(conn); } @Override public void onDestroy() { System.out.println("onDestroy()方法。。。"); super.onDestroy(); } public void Play(){ System.out.println("MyBindService.Play()方法,播放音乐"); } public void Pause(){ System.out.println("MyBindService.Pause()方法,暂停"); } }
有一点要说明:写service或者自己定义了一个新的activity等,这些都需要在清单文件里进行注册。
否则不能够生效,有的时候程序还不报错,页面上还只是空白,所以不好找原因。要牢记一定在在清单文件里注册。