BindService的生命週期以及使用方法。
阿新 • • 發佈:2019-01-31
bindService():允許其他元件跟它進行通訊,允許多個客戶端繫結到同一個service上,當所有的客戶端都解除繫結後,該service就銷燬了。
1、main.xml包括bindservice和unbindservice2個按鈕,還有模擬的四個按鈕
<?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/bind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BindService" /> <Button android:id="@+id/play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放" /> <Button android:id="@+id/pause" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="暫停" /> <Button android:id="@+id/next" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下一首" /> <Button android:id="@+id/pervious" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上一首" /> <Button android:id="@+id/unbind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="UnBindService" /> </LinearLayout>
2、AndroidMainfest.xml宣告Service
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.huangyi.bindservicedemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <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.huangyi.bindservicedemo.BindService"/> </application> </manifest>
3、BindService.java裡面定義了bindService生命週期的幾個方法onCreate----->onBind--->onUnbind---->onDestroy,還自定義了一個類MyBinder整合Binder這個類,並在這個類裡面寫一個方法返回BindService類的物件。當然還有模擬播放的幾個方法。
package com.huangyi.bindservicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindService extends Service { @Override public void onCreate() { Log.i("info", "BindService--->onCreate()"); super.onCreate(); } @Override public IBinder onBind(Intent intent) { Log.i("info", "BindService--->onBind()"); return new MyBinder(); } @Override public boolean onUnbind(Intent intent) { Log.i("info", "BindService--->onUnbind()"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.i("info", "BindService--->onDestroy()"); super.onDestroy(); } public class MyBinder extends Binder{ public BindService getService(){ return BindService.this; } } public void play(){ Log.i("info", "播放"); } public void pause(){ Log.i("info", "暫停"); } public void next(){ Log.i("info", "下一首"); } public void pervious(){ Log.i("info", "上一首"); } }
4、MainActivity.java
其中定義了ServiceConnection介面的實現物件conn,將傳遞過來的binder物件轉化為BindService的物件。
package com.huangyi.bindservicedemo;
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.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button bind,play,pause,next,pervious,unbind;
private Intent intent;
BindService service;
ServiceConnection conn = new ServiceConnection() {
/**
* 當啟動源跟Service的連線意外丟失的時候會呼叫這個方法
* ,比如Service崩潰了或者被強行殺死了
*/
@Override
public void onServiceDisconnected(ComponentName name) {
}
/**
* 當啟動源跟Service成功連線之後將會呼叫這個方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((BindService.MyBinder)binder).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bind = (Button) findViewById(R.id.bind);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
next = (Button) findViewById(R.id.next);
pervious = (Button) findViewById(R.id.pervious);
unbind = (Button) findViewById(R.id.unbind);
bind.setOnClickListener(this);
play.setOnClickListener(this);
pause.setOnClickListener(this);
next.setOnClickListener(this);
pervious.setOnClickListener(this);
unbind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bind:
intent = new Intent(MainActivity.this, BindService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.play:
service.play();
break;
case R.id.pause:
service.pause();
break;
case R.id.next:
service.next();
break;
case R.id.pervious:
service.pervious();
break;
case R.id.unbind :
unbindService(conn);
break;
}
}
}
執行結果:
依次從上到下點選按鈕
程式碼地址:http://download.csdn.net/detail/yihuangol/9259295