安卓 service
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return new Binder(); //这里必须要返回一个Binder实例
}
// 服务启动
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
// 服务销毁
@Override
public void onDestroy() {
super.onDestroy();
}
// 服务开始的时候 执行的方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.print("服务正在执行.....");
new Thread() {
@Override
public void run() {
super.run();
while (true) {
System.out.print("服务正在执行....");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
}
public class MainActivity extends AppCompatActivity implements ServiceConnection {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main); // 创建视图
//setContentView(R.layout.my_layout);
this.setContentView(R.layout.my_layout);
// 启动服务
findViewById(R.id.btnStartServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.print("服务开始执行......");
Intent i = new Intent(MainActivity.this, MyService.class);
startService(i);
}
});
// 停止服务
this.findViewById(R.id.btnStopServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
stopService(i);
}
});
//btnBindServcie 绑定service
this.findViewById(R.id.btnBindServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
// 这里的MainActivity必须要实现ServiceConnection 重写onServiceConnected 和 onServiceDisconnected 方法
bindService(i, MainActivity.this, Context.BIND_AUTO_CREATE);
}
});
//btnBindServcie 解除绑定service
this.findViewById(R.id.btnUnBindServcie).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
unbindService(MainActivity.this);
}
});
System.out.println("onCreate");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("onRestart");
}
// 服务被绑定成功后被执行
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.print("service connected");
}
// 服务被杀掉后执行
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.print("service DisConnected");
}
}
这块不好理解,这个只能在以后的项目里面好好理解了。
安卓 service的更多相关文章
- 安卓Service完全解析(上)
版权声明:本文出自汪磊的博客,转载请务必注明出处. 关于安卓Service相信很多安卓开发者都听说过,作为安卓四大组件之一,即使不经常用也应该听说过,但并不是每一个人都掌握的特别详细,全面.那么今天我 ...
- 安卓Service完全解析(中)
摘要: 版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇中我们学习了Android Service相关的许多基础但是重要的内容,基本涵盖大部分平日里的开发工作.今天我们继续学习一下稍微高级 ...
- 安卓四大组件的作用、安卓Service的作用
Activity好像是应用程式的眼睛,提供与user互动之窗. BroadcastReceiver好像是耳朵,接收来自各方的Intent. Service是在后台运行的. 一个Service 是一段长 ...
- Android Service 文档
应用场景: 1 用于将后台逻辑(Service中)和UI逻辑(Activity中)进行解耦,实现Service功能的复用,为其他程序提供功能. 2 后台功能,由于Activity在进入后台时(On ...
- delphi 10 seattle 安卓服务开发(一)
从delphi 开始支持安卓的开发开始, 安卓service 开发一直都是delphier 绕不过去的坎, 以前也有开发service 的方法,但是都是手工处理启动文件,而且要修改很多东西,基本上成 ...
- Android服务Service
安卓Service服务 一 Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...
- 【原创】如何用Android Studio断点安卓自带Service或Bind类型的Service
很久以来,我一直想找一种方法来断点调试安卓系统自身的Service,或者bind类型的Service,比如我想看WifiManager里面的getWifiApConfiguration函数是如何实现的 ...
- 安卓第十三天笔记-服务(Service)
安卓第十三天笔记-服务(Service) Servcie服务 1.服务概念 服务 windows 服务没有界面,一直运行在后台, 运行在独立的一个进程里面 android 服务没有界面,一直运行在后台 ...
- 安卓四大组件之--service
服务:长期后台运行的没有界面的activity,程序写法和activity类似. 安卓系统进程管理是按照一定规则的: 1.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...
随机推荐
- Atitit. 查找linux 项目源码位置
Atitit. 查找linux 项目源码位置 1. netstat -anp |grep 801 1.1. 1.3 启动关闭nginx3 1.2. 找到nginx配置文件4 1.3. ./etc/ ...
- Android—初识AsyncTask
AsyncTask是用来处理一些后台的比较耗时的任务,给用户带来良好的体验.AsyncTask扩展Thread,增强了与主线程的交互能力. 首先介绍AsyncTask中定义的以下几个方法: onPre ...
- IOS开发札记(2015-08-20)
View显示数据借助Model的一个比较好的理由也是因为:有时候从服务器获取的数据相同的value可能对应不同的key(服务端多人协作开发时经常会出现这种情况) 这里盗图描述一下使用Model的好处 ...
- EditText 显示明文和密码
1.效果图 2.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml ...
- OpenGL渲染流程
一.什么是openGL OpenGL被定义为“图形硬件的一种软件接口”.从本质上说,它是一个3D图形和模型库,具有高度的可移植性,具有非常快的速度. 二.管线 管线这个术语描述了opengl渲染的整个 ...
- 利用ShareSDK进行第三方登录和分享
到相应开发者平台注册开发者账号,并添加你要进行分享和使用第三方登录应用的信息. 添加新浪微博应用 注册网址 http://open.weibo.com添加QQ应用 注册网址 http://mobil ...
- Ubuntu16.04LTS国内快速源
一.源文件位置 备份并替换/etc/apt/sources.list的源内容: 二.更改源文件内容 sudo vi /etc/apt/sources.list deb http://mirrors.a ...
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- vim vundle 安装Base16 Vim主题
1.vim /etc/vimrc set background=dark colorscheme base16-default 2.同样在vimrc中的vundle位置添加 Plugin 'chris ...
- 【转】Hive的insert操作
insert 语法格式为: 1. 基本的插入语法: insert overwrite table tablename [partition(partcol1=val1,partclo2=val2)] ...