service详解
2015-11-03 16:26
197 查看
Thread 主要用来执行异步操作service 是android的一种机制, 比如在后台执行播放音乐之类的生命周期: onCreate onStartCommand onUnbind onDestroyIntent intent = new Intent(ServiceTestActivity.this, MyService.class); startService(intent); // 会调用 onCreate onStartCommand 如果已启动servce 只会调用onStartCommand
public class CountService extends Service { private boolean threadDisable; private int count; @Override public IBinder onBind(Intent intent) { return null; } @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"); } public int getCount() { return count; } }
public class LocalServiceDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.startService(new Intent(this, CountService.class)); } @Override protected void onDestroy() { super.onDestroy(); this.stopService(new Intent(this, CountService.class)); } }
相关文章推荐
- Gone with the wind on women's enlightenment
- ESP8266编译环境 对于习惯Ubuntu默认桌面的用户非常好
- CI自定义js,css的url
- 利用percona-xtrabackup快速搭建MySQL数据库主从复制
- 利用percona-xtrabackup快速搭建MySQL数据库主从复制
- java中的匿名内部类总结
- YII2设置路由访问,美化url
- java mail
- Eclipse署动态web项目方法
- oracle 存储过程编辑 卡死
- 安卓学习教程总结:芯灵思开发板安卓底层开发
- request,response,session,cookie,application
- 使用C++复制文件
- App 开发:Hybrid 架构下的 HTML5 应用加速方案
- 迁移OCR、表决磁盘和asm spfile到其他磁盘组中
- matlab练习程序(奇异值分解压缩图像)
- java list 根据对象一个字段去重
- phpexcel 导出数据
- 当Oracle 11g sql plus界面出现 ORA-12560:TNS:协议配置器错误时的解决办法
- 电商中设置满包邮,满减等促销搞活动;电商平台同步到第三方,日志记录