Android 编写 Service 入门
2010-04-09 06:15:00 来源:WEB开发网package="com.easymorse" android:versionCode="1" android:versionName="1.0">
< android:name=".LocalServiceDemoActivity">
android:label="@string/app_name">
Acitity代码不再通过startSerivce和stopService启动关闭服务,另外,需要通过ServiceConnection的内部类实现来连接Service和Activity。
package com.easymorse;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class LocalServiceDemoActivity extends Activity {
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countService = (ICountService) service;
Log.v("CountService", "on serivce connected, count is "
+ countService.getCount());
}
@Override
public void onServiceDisconnected(ComponentName name) {
countService = null;
}
};
private ICountService countService;
/** Called when the activity is first created. */
赞助商链接