在Activity中

package cn.edu.servicetest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ServiceTestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Button btn=(Button)this.findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(ServiceTestActivity.this, MyService.class);ServiceTestActivity.this.startService(intent);}                });    }}

二、继承Service

package cn.edu.servicetest;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service{@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("service Begin");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}

在AndroidManifest.xml中增加如下配置信息

<service android:name="cn.edu.servicetest.MyService"></service>

更多相关文章

  1. Android(安卓)Seek自定义样式
  2. Android增加自定义监听事件
  3. Android(安卓)Intent列表
  4. Android的版本(Version)和API-level的对应关系
  5. Android入门:Button
  6. Android(安卓)AsyncTask 异步任务取消
  7. android 使用randerScript实现图片模糊效果
  8. Android简单的Fragment嵌套Fragment(Viewpager+Fragement)
  9. android 设置屏幕亮度

随机推荐

  1. android WebView登录状态session id 和co
  2. Android中工作线程与主线程同步方式
  3. Android常用UI组件 - TextView
  4. Android中JNI 的一些常用说明 JNI_OnLoad
  5. Android(java)学习笔记125:Clock app编写报
  6. android xml 分析1--- AndroidManifest.x
  7. Android 关闭线程(转)
  8. android Supporting multiple screen翻译
  9. android中调用系统功能 来显示本地相册图
  10. android小功能实现之发送短信