前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >启动Service

启动Service

作者头像
tea9
发布2022-07-16 15:57:28
发布2022-07-16 15:57:28
9080
举报
文章被收录于专栏:tea9的博客tea9的博客

//1.首先创建一个Service /////MainActivity 在MainActivity中启动服务 //启动服务 findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startService(new Intent(MainActivity.this, MyService.class)); } }); 在MainActivity中停止服务 //停止服务 findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopService(new Intent(MainActivity.this,MyService.class)); } }); ///////// —-MyService //重写onStartCommand方法

//执行到startService中执行这个方法 @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println(“kkk”); new Thread(){ @Override public void run() { super.run(); while (true) { System.out.println(“服务正在运行。。。”); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); return super.onStartCommand(intent, flags, startId); } ///////////////////////////////////

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档