通过android的四大组件之一的service来实现后台运行,类似Windows上的服务。

1、Android上的service有两种启动方式(或者说两种方法实现service)

①startService()和bindService() ,有区别。

2、简单的使用Service步骤(startService()):

①建立service的子类,重写onStartCommand()。(当服务启动的时候会调用该方法)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class HelloService extends Service{ @Override public void onCreate(){ } //这个函数在低版本中使用的是onStart(),onStart()在高版本中已经过时了。 @Override public int onStartCommand(Intentintent, int flags, int startId){ } @Override public void onDestroy(){ } }

②在清单文件中声明Service组件

1 2 3 4 5 6 7 < application > < service android:name = "HelloService" > < intent-fiter > < action android:name = "xxxxx" > </ intent-fiter > </ service > </ application >

③在Activity等调用startService(intent);启动你的Service

1 2 3 Intentintent= new Intent( "xxxxx" ); //还可以使用Intentintent=newIntent(activity.this,HelloService.class); startService(intent);

注:两种方法各有不同,具体请看官方API:

http://developer.android.com/guide/components/services.html

更多相关文章

  1. [Andriod] android中singleTask的home键的问题
  2. Android(安卓)之 Activity (一) 基础知识
  3. 测试cocos2dx tojs时出现找不到android/log.h的解决方法
  4. 你真的了解AsyncTask吗?AsyncTask源码分析
  5. Android之监测database的改变--notifyChange
  6. Android(安卓)view 滑动事件冲突解决方法(理论篇)
  7. Ubuntu下Android(安卓)studio常用快捷键集锦
  8. Android入门之addWindow
  9. 关于使用AccountManager的remove删除Android帐号的细节

随机推荐

  1. Android学习笔记(十四)
  2. 想在Android上用一下PureMVC,谁知结果……
  3. Android(安卓)studio 使用问题汇总
  4. phonegap WebApp
  5. Android之屏幕方向改变以及onConfigurati
  6. Flash移动开发高级教程——创建Anroid &
  7. xamarin开发android收集的一些工具
  8. Android(安卓)webView 中loadData方法加
  9. Android中如何获取系统中所有的应用程序
  10. Android(安卓)INSTALL_FAILED_ACWF_INCOM