Service 中的 onStart 和 onStartCommand
在自定义的service中,写了onStart和onStartCommand,
public class HttpWebService extends Service {
@Override
public void onCreate() {
super.onCreate();
....@Overridepublic void onStart(Intent intent, int startId) {// this will trigger AbstractBackgroundService.onStart()super.onStart(intent, startId);Log.d(LOG_TAG, "onStart" + intent); }@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {int retVal = super.onStartCommand(intent, flags, startId);Log.d(LOG_TAG, "onStartCommand" + intent);return retVal;}}
如果用 bindService 这种方式调用,onStart和onStartCommand都不会被调用到
this.bindService(new Intent(this, HttpWebService.class),new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name,IBinder service) {HttpWebService webService = ((HttpWebService.LocalBinder) service).getService();webService.SetServerListener(webListener);}@Overridepublic void onServiceDisconnected(ComponentName name) {}}, Context.BIND_AUTO_CREATE);
如果是用StartService调用,两个都被调用到
Intent intent = new Intent("com.example.sharefiles.services.ShareServices.Test");intent.setClass(getApplicationContext(), HttpWebService.class); intent.putExtra("enable", true);this.startService(intent);
查看google 文档
http://developer.android.com/reference/android/app/Service.html
onStart(Intent intent, int startId)
onStartCommand(Intent, int, int)
instead.
onStartCommand(Intent intent, int flags, int startId)
startService(Intent)
, providing the arguments it supplied and a unique integer token representing the start request.
鉴于目前的代码基本都是运行在API5 以上的,所以直接干掉onStart.
Service 中的 onStart 和 onStartCommand相关推荐
- Service中的绑定服务总结
绑定服务是客户端服务器接口中的服务器,绑定服务可以让组件绑定到服务.发送请求.接收响应,甚至执行进程间通信IPC,绑定服务通常只在为其他应用组件服务时处于活动状态,不会无限期在后台运行. 绑定服务是S ...
- 使用IntentService在Service中创建耗时任务
IntentService是Service的子类,比普通的Service增加了额外的功能. Service存在两个问题: 1,Service不会专门启动一条单独的线程,Service与它所在应用访问者 ...
- 在后台Service中直接启动Activity
如果你有Service在跑,但是没有处于resume的activity,这时候如果你想启动一个Activty,比如提醒用户升级应用,在后台Service中是可以直接启动Activity的 如果用的co ...
- Handler: Service中使用Toast
Handler 的使用在 android App 开发中用的颇多,它的作用也很大,使用 Handler 一般也会使用到多线程,相信大家对 Handler 不会陌生,在这里,重点说一下 android ...
- 窗口设置Service中添加 浮动 View
废话就不多说了,开始... 一般构建UI的时候都是通过Actvity.setContentView(R.layout.main) 来实现的,其等价于Activity.getWindow().setCo ...
- Android 在Service中使用bindService
前面已经对Service的startServer方式启动一个服务了解过了,现在来看一下Service的另一种启动方式→bindServer bindServer使用场景 1.在同个app之间调用(即是 ...
- Service中bindService
最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...
- Android在service中实现随机数产生
1.在service中实现随机数产生: 2.实现Service中的各个生命周期函数,并理解其功能: 2.在Activity界面实现随机数的显示,每2秒更新一次: 3.采用启动式完成service的启动 ...
- 在Service中通过WindowManger添加View的方式来把UI界面显示出来
整体方案 在Service中通过WindowManger添加View的方式来把UI界面显示出来 业务场景 具体场景 IQOO手机,游戏辅助 这种场景能否使用Activity方式来做 使用activit ...
- 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast
在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast 1. 表象 Service中可以正常显示Toast,IntentServic ...
最新文章
- 033_jQuery Ajax的ajax方法
- selenium webdriver (12) -- 鼠标和键盘
- Android事件分发小结
- 第四章 前端开发——JQuery库
- Python从序列中选择k个不重复元素
- 条目十四《使用reserve来避免不必要的重新分配》
- 调查 10,500 名 Java 开发者发现,收费的 OracleJDK 仍是主流、IntelliJ IDEA 最受欢迎...
- [Java] 蓝桥杯ALGO-100 算法训练 整除问题
- Django根据现有数据库建立/更新model
- 新中大软件 java班不_新中大gsoft-12.0-软件安装说明.doc
- win7系统gpt分区激活工具哪个好?
- 借鸡生蛋术–砍价小程序的推广变现教学
- python爬虫设计实验
- PHP腾讯云短信接口
- Schematic export failed or was cancelled. Please consult the transcript in the source windo
- 学完了C++语法之后该学什么??(网络基础篇)
- 暑假多看看英文原版电影
- linux rpm -qip命令,linux rpm命令
- win10系统麦克风声音太小怎么设置
- objectArx ---基础操作