安卓 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.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...
随机推荐
- 【原】iOS动态性(五)一种可复用且解耦的用户统计实现(运行时Runtime)
声明:本文是本人 编程小翁 原创,转载请注明. 为了达到更好的阅读效果,强烈建议跳转到这里查看文章. iOS动态性是我的关于iOS运行时的系列文章,由浅入深,从理论到实践.本文是第5篇.有兴趣可以看看 ...
- Objective-C内存管理之-引用计数
本文会继续深入学习OC内存管理,内容主要参考iOS高级编程,Objective-C基础教程,疯狂iOS讲义,是我学习内存管理的笔记 内存管理 1 内存管理的基本概念 1.1 Objective-C中的 ...
- 解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
在Windows Server 2008 x64 上部署一个Vs 2008开发的.net2.0 的asp.net web 程序,调用了office的组件来导入导出excel文件,其中托管管道模式为集成 ...
- ref
ref: 当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中. 例如: class RefExample { //使用ref返回的函数 static void Method(ref ...
- p2p tcp nat 原理图+源码(原创)
现今网上p2p的 udp nat穿透 文章 多如牛毛, p2p tcp nat的文章寥寥无几 ,up主研究了几天 终于有所收获,特来向大家分享,请大家多多支持! 1.首先你要有台外网服务器 或者 电信 ...
- 手把手教你编译安装MariaDB
MariaDB是什么? MariaDB是MySQL的一个分支,由于Oracle有可能对MySQL闭源,所以分离了出来(MySQL先后被Sun.Oracle收购). 但是除了作为一个Mysql的&quo ...
- 烂泥:阿里云RDS本地恢复数据
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司目前使用的数据库是阿里云的RDS,目前RDS的版本为mysql5.6.如下: 现在要 ...
- linux init 启动顺序
redhat init大致启动过程 第一个运行的程序是/sbin/init,该文件会读取/etc/inittab文件,并依据此文件来进行初始化工作.比如在设定了运行等级 “:id:3:initdefa ...
- 使用好压(HaoZip)软件打包EverEdit制作安装程序
最近使用EverEdit,使用原始的安装程序安装后,需要重新安装插件,对配置文件进行了修改,定制了工具栏.将安装后的程序目录进行打包,制作新的安装包,便于携带. 以下为打包制作过程: 打包原料:Eve ...
- 报表移动端app如何实现页面自适应?
1. 描述 PC上制作好的报表,在手机端查看的时候,报表软件默认的自适应效果不尽人如意.例如,报表比较大,到手机上被缩的非常小,字都看不清等等.为此FineReport增加了选项可以手动控制报表在移动 ...