標籤:android開發 綁定服務 binder 遊響雲停工作室
綁定服務是在android中局部使用的服務,用戶端和服務是在同一進程中工作的,不需要跨進程操作。用戶端通過bindService方法與服務建立關聯
下面這個例子示範用戶端調用服務的擷取時間方法
1.建立綁定服務
首先建立服務,
服務中建立一個內部類TimeBinder,繼承Binder,通過Onbind回調返回服務執行個體,
在服務中建立一個擷取時間的公用方法,供用戶端調用
package com.example.helloword;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Binder;import android.os.IBinder;public class BindService extends Service {private final IBinder binder= new TimeBinder();//建立一個內部綁定類 public class TimeBinder extends Binder { BindService getService() { // Return this instance of LocalService so clients can call public methods return BindService.this; } } @Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn binder;} /*只在建立時執行一次*/ @Override public void onCreate() { super.onCreate(); } /*斷開綁定或者stopService時執行*/ @Override public void onDestroy() { super.onDestroy(); } /* 當從新嘗試綁定時執行 */ @Override public void onRebind(Intent intent) { super.onRebind(intent); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } /* ,每次startService都會執行該方法,而改方法執行後會自動執行onStart()方法 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } //擷取時間public String getcurtime(){//日期格式 化SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");Date date = new Date();return format.format(date.getTime());}}
2.調用綁定服務
在用戶端,通過bindservice綁定服務
<?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/btngettime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="擷取時間" /> <TextView android:id="@+id/tvtime" android:layout_width="262dp" android:layout_height="wrap_content" android:text="TextView" /></LinearLayout>
用戶端重寫ServiceConnection的兩個方法。 onServiceConnected系統調用這個來傳遞在服務onBind 中返回的IBinder OnServiceDisconnected() 在與系統串連意外丟失,調用這個
package com.example.helloword;import com.example.helloword.BindService.TimeBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;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;import android.widget.TextView;public class BindServiceActivity extends Activity { private Button btntime;//擷取時間private TextView tvtime;//顯示時間BindService bindservice;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.bindservicelayout);btntime=(Button)findViewById(R.id.btngettime);tvtime=(TextView)findViewById(R.id.tvtime);btntime.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubtvtime.setText(bindservice.getcurtime());}});}@Override protected void onStart() { super.onStart(); // Bind to LocalService Intent intent = new Intent(this, BindService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); }private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance TimeBinder binder = (TimeBinder)service; bindservice=binder.getService(); } @Override public void onServiceDisconnected(ComponentName arg0) { bindservice=null; } }; }
androidmanifest.xml
<activity android:name="com.example.helloword.BindServiceActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".BindService"> <intent-filter> <action android:name="com.example.helloword.BindService" /> <category android:name="android.intent.category.default" /> </intent-filter> </service>
.Net程式員玩轉Android開發---(20)Android綁定服務