Start方式的特点:
服务跟启动源没有任何联系
无法的带服务对象
Bind方式的特点:
通过Ibinder接口实例,返回一个ServiceConnection对象给启动源
通过ServiceConnection对象的相关方法可以得到Service对象
package com.example.servicedemo;
import com.example.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.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
Intent intent1;
Intent intent2;
MyBindService service;
ServiceConnection conn=new ServiceConnection() {
//当启动源跟Service的链接意外丢失的时候会调用这个方法
//比如当Service崩溃了或被强行杀死了
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
//当启动源跟Service连接成功之后会自动调用这个方法
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
// TODO Auto-generated method stub
service=((MyBinder)binder).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View view){
switch (view.getId()) {
case R.id.start:
intent1=new Intent(MainActivity.this,MySyartService.class);
startService(intent1);
break;
case R.id.stop:
stopService(intent1);
break;
case R.id.bind:
intent2=new Intent(MainActivity.this,MyBindService.class);
bindService(intent2,conn,Service.BIND_AUTO_CREATE);
break;
case R.id.unbind:
unbindService(conn);
break;
case R.id.play:
service.play();
break;
case R.id.next:
service.next();
break;
case R.id.push:
service.push();
break;
case R.id.pervious:
service.pervious();
break;
default:
break;
}
}
}
package com.example.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MySyartService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("Info","service--onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("Info","service--onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Info","service--onDestroy()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("Info","service--onBind()");
return null;
}
}
package com.example.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyBindService extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("Info","BindService-onCreate()");
}
public class MyBinder extends Binder{
public MyBindService getService(){
return MyBindService.this;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Info","BindService-onDestroy()");
}
@Override
public void unbindService(ServiceConnection conn) {
// TODO Auto-generated method stub
super.unbindService(conn);
Log.i("Info","BindService-unbindService()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("Info","BindService-onBind()");
return new MyBinder();
}
public void play(){
Log.i("Infor","播放");
}
public void pervious(){
Log.i("Infor","上一首");
}
public void push(){
Log.i("Infor","暂停");
}
public void next(){
Log.i("Infor","下一首");
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.servicedemo.MainActivity" >
<Button
android:id="@+id/pervious"
android:onClick="doClick"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/push"
android:layout_below="@+id/next"
android:text="上一首" />
<Button
android:id="@+id/next"
android:onClick="doClick"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/push"
android:text="下一首" />
<Button
android:id="@+id/unbind"
android:onClick="doClick"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/pervious"
android:layout_below="@+id/pervious"
android:text="UnBindService" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Start:"
android:textSize="20dp" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:onClick="doClick"
android:text="startService" />
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:onClick="doClick"
android:text="StopService" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/stop"
android:text="Bind:"
android:textSize="20dp" />
<Button
android:id="@+id/bind"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/stop"
android:layout_below="@+id/textView2"
android:onClick="doClick"
android:text="BindService" />
<Button
android:id="@+id/push"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textSize="10dp"
android:layout_alignLeft="@+id/play"
android:layout_below="@+id/play"
android:onClick="doClick"
android:text="暂停" />
<Button
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignRight="@+id/bind"
android:layout_below="@+id/bind"
android:onClick="doClick"
android:text="播放"
android:textSize="10dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.servicedemo.MySyartService"
></service>
<service
android:name="com.example.servicedemo.MyBindService"
></service>
</application>
</manifest>