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。
