1. 建立一個android項目如下,點擊finish. 如果只是需要提供service,不需要Activity,那麼可以把Create Activity的勾去掉。
2. 添加一java檔案。
類名為:LongExistService,
superclass為:android.app.Service
3. 開啟LongExistService.java, 點擊菜單 Source-> Override/implement Method.
選中onCreate(), onDestroy(), onStart(), onStartCommand(), onUnbind().
4. LongExistService.java檔案內容為:
package com.pyk.les;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class LongExistService extends Service {
public int getCount() {
Log.d(TAG, "LongExistService::getCount");
return 0;
}
public String getTime() {
java.util.Date date = new java.util.Date();
Log.d(TAG, "LongExistService::onCreate");
return date.toLocaleString();
}
private final IlongExistService.Stub binder=new IlongExistService.Stub() {
public int getCount() {
Log.d(TAG, "IlongExistService::getCount");
return LongExistService.this.getCount();
}
public String getTime() {
Log.d(TAG, "IlongExistService::getTime");
return LongExistService2.this.getTime();
}
};
final String TAG = "LongExistService";
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind");
super.onRebind(intent);
}
@Override
public IBinder onBind(Intent arg0) {
Log.d(TAG, "onBind");
return binder;
}
}
5. 因為增加了AIDL的介面,我們需要先定義一個AIDL檔案,比如IlongExistService.aidl.檔案內容為:
package com.pyk.les;
interface IlongExistService {
int getCount();
String getTime();
}
這裡必須是尾碼為aidl,我試過用.java檔案定義一個同樣的介面,結果不能正常運行。所以這個看來是個必須起名為aidl檔案的要求。
6.修改AndroidManifest.xml, 添加如下定義。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pyk.les2" android:versionCode="1" android:versionName="1.0">
<application android:label="Single Service">
<service android:name=".LongExistService">
<intent-filter>
<action android:name="com.pyk.les.IlongExistService"></action>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
7.到此,service已經實現了。下面我們另外產生一個項目的Activity來bind這個service。
按照前面的步驟1. 我們產生一個activity。注意請用另外一個package name。Activity.java的檔案內容為:
package com.pyk.client;
import com.pyk.les.IlongExistService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
public class MyActivity extends Activity {
final String TAG = "MyActivity";
final Intent myIntent = new Intent("com.pyk.les.IlongExistService");
private boolean startedService = false;
private IlongExistService leservice = null;
ServiceConnection myServiceConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName name, IBinder service) {
leservice = IlongExistService.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName name) {
leservice = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//myIntent.setClass(this, LongExistService.class);
startedService = bindService(new Intent("com.pyk.les.IlongExistService"), myServiceConnection, BIND_AUTO_CREATE);
try {
Log.i(TAG, "getCount value:" + leservice.getCount());
Log.i(TAG, "getTime value:" + leservice.getTime());
}catch (RemoteException ex)
{}
}
@Override
protected void onDestroy() {
if (startedService)
{
unbindService(myServiceConnection);
}
Log.i(TAG, "onDestroy");
super.onDestroy();
}
}
8. 將IlongExistService.aidl拷貝過來,連同package的目錄一起拷貝。然後重新整理一下Activity項目,就能在project explorer裡看到了。
這樣,當Activity啟動時,就會bind service並調用service的方法。Activity退出時會unbind。