WEB开发网
开发学院手机开发Android 开发 Android 编写 Service 入门 阅读

Android 编写 Service 入门

 2010-04-09 06:15:00 来源:WEB开发网   
核心提示:这是新建立的接口代码:package com.easymorse;public interface ICountService {public abstract int getCount();}修改后的CountService代码:package com.easymorse;import android.app.Ser

这是新建立的接口代码:

package com.easymorse;

public interface ICountService {

public abstract int getCount();

}

修改后的CountService代码:

package com.easymorse;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

public class CountService extends Service implements ICountService {

private boolean threadDisable;

private int count;

private ServiceBinder serviceBinder=new ServiceBinder();

public class ServiceBinder extends Binder implements ICountService{

@Override

public int getCount() {

return count;

}

}

@Override

public IBinder onBind(Intent intent) {

return serviceBinder;

}

@Override

public void onCreate() {

super.onCreate();

new Thread(new Runnable() {

@Override

public void run() {

while (!threadDisable) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

}

count++;

Log.v("CountService", "Count is " + count);

}

}

}).start();

}

@Override

public void onDestroy() {

super.onDestroy();

this.threadDisable = true;

Log.v("CountService", "on destroy");

}

/* (non-Javadoc)

* @see com.easymorse.ICountService#getCount()

*/

public int getCount() {

return count;

}

}

服务的注册也要做改动,AndroidManifest.xml文件:

< xmlns:android="http://schemas.android.com/apk/res/android">

上一页  1 2 3 4 5 6 7  下一页

Tags:Android 编写 Service

编辑录入:coldstar [复制链接] [打 印]
赞助商链接