service类中:
public class Music extends Service {
private MediaPlayer player;
private myBinder mybinder;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mybinder=new myBinder();
}
/*
* 用于绑定activity和local service的通信
*/
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mybinder;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
player=MediaPlayer.create(getApplication(), R.raw.long2);
player.start();
Log.e("Log", "start");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Log.e("Log", "stop");
}
/*
* Binder实现了IBinder,集成Binder较方便
*/
public class myBinder extends Binder{
//获取service实例
public Music getService(){
return Music.this;
}
//此方法仅为测试用
public int add(int a,int b){
return a+b;
}
}
}
activity类中:
public class MainActivity extends Activity implements OnClickListener{
private ServiceConnection scConnection;
private Button start,binder,unbinder,stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
binder=(Button)findViewById(R.id.binder);
unbinder=(Button)findViewById(R.id.unbinder);
stop=(Button)findViewById(R.id.stop);
start.setOnClickListener(this);
binder.setOnClickListener(this);
unbinder.setOnClickListener(this);
stop.setOnClickListener(this);
scConnection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
Music.myBinder binder=(Music.myBinder)arg1;
Log.e("Log", binder.add(2, 4)+"");
}
};
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
//创建service
case R.id.start:
Toast.makeText(getApplication(), "start", Toast.LENGTH_LONG).show();
startService(new Intent(MainActivity.this,Music.class));
break;
//绑定service
case R.id.binder:
bindService(new Intent(MainActivity.this, Music.class), scConnection, Context.BIND_AUTO_CREATE);
break;
//解绑service
case R.id.unbinder:
unbindService(scConnection);
break;
//停止service
case R.id.stop:
Toast.makeText(getApplication(), "stop", Toast.LENGTH_LONG).show();
stopService(new Intent(MainActivity.this,Music.class));
break;
default:
break;
}
}
}
xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
<Button android:id="@+id/binder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定"/>
<Button android:id="@+id/unbinder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑"/>
<Button android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束"/>
</LinearLayout>
配置文件中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo10"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="demo10.MainActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="demo10.Music">
</service>
</application>
</manifest>
项目名demo10,包名demo10;音乐用的.mp3文件。在res文件下新建的raw文件。调用R.raw.文件名。