1 . onServiceDisconnected 何時有CallBack :
並非 unBindService 就會觸發,此回應是Service 被異常銷毀(資源不足...等)
2 . IPC : 進程之間的通信 ( activity 和 Service 之間的參數傳遞 )
3 . activity onDestory 後 bindService 所執行的 Service 也會跟者終止
-------------------------------------
導讀 :
1 . 建立帶有 Bind 的 Service
2 . 在 Activity 內建立 ServiceConnection 物件
在 Connection 的 CallBack 將 Service 建立出來
3 . 啟動 Service 和 帶入剛剛生成的物件
4 . 可控制 Service 了
This is a easy Sample
範例 :
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bind" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ServiceMethod" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unBind" />
</LinearLayout>
MainActivity.java
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.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
private String TAG = "MainService";
private Button btn_1;
private Button btn_2;
private Button btn_3;
public MainService myService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1 = (Button)findViewById(R.id.button1);
btn_2 = (Button)findViewById(R.id.button2);
btn_3 = (Button)findViewById(R.id.button3);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.button1:
// 綁定 Service
Intent serviceIntent = new Intent(this, MainService.class);
this.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
break;
case R.id.button2:
// 使用 Service 內的方法
myService.uu();
break;
case R.id.button3:
// 解除綁定 Service
try{
this.unbindService(connection);
} catch(Exception e){
Log.d(TAG, e.toString());
}
break;
}
}
public ServiceConnection connection = new ServiceConnection() {
// 成功與 Service 建立連線
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MainService.MyBinder) service).getService();
Log.d(TAG, "MainActivity onServiceConnected");
}
// 與 Service 建立連線失敗
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
Log.d(TAG, "MainActivity onServiceFailed");
}
};
}
MainService.java
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
private String TAG = "MainService";
public MyBinder myBinder = new MyBinder();
// 綁定此 Service 的物件
public class MyBinder extends Binder {
public MainService getService() {
return MainService.this;
}
}
// 綁定
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "MainService onBind");
return myBinder;
}
// 解除綁定
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "MainService onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "MainService onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "MainService onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainService onDestroy");
}
// Service 測試用的 Method
public void uu(){
Log.d(TAG, "MainService uu");
}
}
Finish :
參考資料 : 來源
參考資料 : 程式碼
歡迎轉載,請註明出處。
沒有留言:
張貼留言