編輯:關於 Android 程式設計
在 Android 裡,Service 的資料綁定是一種相當實用的手法。由於 Service 與 Activity 一樣運行在應用行程的主線程中,兩者之間有多種互動方式,本文就來說明如何透過「綁定」讓 Service 把資料回傳給 Activity。
先宣告一個介面,把需要對外開放的方法定義出來:
public interface ICount {
public int getcount();
}
接著讓 Service 子類實作這個介面,並定義一個 ServiceBinder 內部類,透過它的物件來綁定資料。要特別注意的是:Service 本身就跑在主線程,若要做耗時操作,仍必須自行開啟子執行緒。至於 onBind() 的回傳型別是 IBinder,而我們的 ServiceBinder 繼承自 Binder——這是因為 Binder 正是 IBinder 的直接子類,是輕量級遠端程序呼叫機制的基礎。
public class BackGroundService extends Service implements ICount {
private boolean disable; // 線程是否執行的標識位
private int count; // 計數
private ServiceBinder serviceBinder = new ServiceBinder();
public class ServiceBinder extends Binder implements ICount {
@Override
public int getcount() {
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (!disable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
count++;
System.out.println("CountService Count is " + count);
}
}
}).start();
}
@Override
public int getcount() {
return count;
}
@Override
public void onDestroy() {
this.disable = true;
System.out.println("Service destroy...");
super.onDestroy();
}
}
這裡不只聲明 Service,還要為它加上 intent 的過濾器,供後續 bind 時比對:
<service android:name="com.yqc.testservice.BackGroundService">
<intent-filter>
<action android:name="com.yqc.testservice.BackGroundService" />
</intent-filter>
</service>
前面定義的 intent 過濾器,正是拿來當作 bindService() 的參數。透過 ServiceConnection 與 Service 建立連線,並在連上時取得後台資料:
public class MainActivity extends Activity {
Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.bindService(new Intent("com.yqc.testservice.BackGroundService"),
this.serviceConnection, BIND_AUTO_CREATE);
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { }
});
}
@Override
protected void onDestroy() {
this.unbindService(serviceConnection);
System.out.println("Activity Destroy...");
super.onDestroy();
}
private ICount countentity;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countentity = (ICount) service;
System.out.println("CountService on service connected, count is " + countentity.getcount());
}
@Override
public void onServiceDisconnected(ComponentName name) {
countentity = null;
}
};
}
關鍵在於 onServiceConnected():當連線建立,我們把回傳的 IBinder 轉型成 ICount,此後就能直接呼叫 Service 暴露的方法取得資料。別忘了在 onDestroy() 中呼叫 unbindService() 解除綁定,避免資源洩漏。
踩坑筆記:Android Service 中 Toast 不顯示的原因與解法
做 Service 練習時,我在 onCreate、onStart、onDestroy 三個方法裡都像 Activity 那樣呼叫了 Toast.makeText,並用兩
微信理財通怎麼買?從綁定銀行卡到收益入帳
使用微信理財通前,需先在微信錢包綁定銀行卡,且零錢餘額要足夠。進入我、錢包、理財通,了解產品後選擇買入。 填寫金額並同意協議,選擇銀行卡支付。完成後可查看收益到帳時間。
Android 逆向工程安全透視:從哈皮妹案例看防竄改設計
從案例看 Android 安全邊界 本文以一款互動娛樂應用為例,討論 Android 逆向工程可能帶來的風險。該應用部分內容需要付費解鎖,若缺乏保護機制,容易被重新打包或
Android 入門:用 DisplayMetrics 獲取手機屏幕分辨率
本文示範如何獲取手機屏幕大小。核心用到了三個物件:TextView、Button,以及關鍵類 DisplayMetrics。例子非常簡單——點擊按鈕觸發事件,然後在 Te