我的安卓记录二(service)
来源:互联网 发布:淘宝网买四轮电动车 编辑:程序博客网 时间:2024/05/01 17:54
service在后台运行
生命周期:onCreate()–>onStart()–>onDestroy()
1、新建一个service类
package com.example.wlb.launchlocalapp;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class MyService extends Service { public MyService() { } private boolean serviceRunning = false; @Override public IBinder onBind(Intent intent) { return new Binder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("Service onCommand."); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); System.out.println("Service onCreate."); serviceRunning = true; new Thread(){ @Override public void run() { super.run(); while (serviceRunning) { System.out.println("Service is running..."); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service onDestory."); serviceRunning = false; }}
2、启动/停止Service
Intent intent = new Intent(MainActivity.this,MyService.class);.....startService(intent)stopService(intent)
3、onCreate()用于启动Service
public void onCreate() { super.onCreate(); System.out.println("Service onCreate."); serviceRunning = true; new Thread(){ @Override public void run() { super.run(); while (serviceRunning) { System.out.println("Service is running..."); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); }
4、onDestroy()用于停止Service
public void onDestroy() { super.onDestroy(); System.out.println("Service onDestory."); serviceRunning = false; }
5、onStartCommand在Service中传数据
(1)创建editText用于输入数据
public void onClick(View v) { switch (v.getId()){ case R.id.btnStartService: intent.putExtra("data",editText.getText().toString()); startService(intent); break; case R.id.btnStoptService: stopService(intent); break; case R.id.btnBindtService: bindService(intent, this, Context.BIND_AUTO_CREATE); break; case R.id.btnUnbindtService: unbindService(this); break; } }
(2)通过intent传递数据
@Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("Service onCommand."); data = intent.getStringExtra("data"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); System.out.println("Service onCreate."); serviceRunning = true; new Thread(){ @Override public void run() { super.run(); while (serviceRunning) { System.out.println(data); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service onDestory."); serviceRunning = false;
0 0
- 我的安卓记录二(service)
- 安卓Service详解(二)
- 我的安卓手机上的垃圾文件清理记录
- 记录我的新工作(二)
- 我的Hibernate学习记录(二)
- 我的Spring学习记录(二)
- 安卓核心组件二service
- 记录我的网安生涯
- 我的安卓
- 我在这记录我的“安卓程序员”开发之路
- 背景音乐(安卓Service)
- 安卓 服务(Service)
- 记录我的VHDL学习之路(二)
- 我的three.js学习记录(二)
- 安卓学习笔记(九)、service的使用
- 我的博客开通啦!记录安卓开发的点点滴滴.
- 我的网站被K记录二
- 这篇文章负责记录安卓开发的规范 我会持续更新
- Android 手动显示和隐藏软键盘
- 网络层01
- vs 为函数自动添加注释
- Js 变量声明提升和函数声明提升
- Java关于日期时间的工具类
- 我的安卓记录二(service)
- 【LeetCode】6. ZigZag Conversion
- js中基础数据结构数组去重问题
- QT 模态与非模态对话框的创建与退出
- SQLServer2008中的时间、日期函数举例
- ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.165' (113)
- Oracle 表空间和用户权限管理
- Java关于字符串String的工具方法
- MapReduce原理与设计思想