安卓 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.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...
随机推荐
- Android—Bundle传递ArrayList<T>
Android开发中Activity传值特别普遍,最贱开发需要传递集合List到另一个Activity,在此作出总结. 首先创建自己的实体类:我的暂命名为Gate. 声明List集合时候泛型中是你声明 ...
- Android中的AlertDialog使用示例一(警告对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Set up Github Pages with Hexo, migrating from Jekyll
Set up Github Pages with Hexo, migrating from Jekyll. 本文介绍用Hexo建立github pages, 其中包含了从Jekyll迁移过来的过程. ...
- Linux0.11内核--fork进程分析
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5597818.html ] 据说安卓应用里通过fork子进程的方式可以防止应用被杀,大概原理就是 ...
- iOS 疑难杂症 — — UIButton 点击卡顿/延迟
前言 一开始还以为代码写的有问题,点击事件里面有比较耗时卡主线程的代码,逐一删减代码发现并不是这么回事. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.c ...
- Android Studio 恢复小窗口停靠模式(Docked Mode)
安卓studio在使用小窗口时,如果我们点击取消了窗口的docked mode模式,窗口就会变成,你一旦触发窗口以外的区域,窗口就会龟缩回去.此时,如果你想要恢复回原来的docked mode的话,具 ...
- SSDT旧版本对于xml数据的处理BUG
在早期版本中,CLR中声明sqlxml时,SSDT会将CLR函数中使用的类型解析为NVARCHAR(4000),最终导致传向CLR函数的数据不完整
- centos7安装vncserver
:# yum install tigervnc-server -y :cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vnc ...
- SQL SERVER 2000 迁移后SQL SERVER代理服务启动错误分析
公司有一个老系统,这个系统所用的数据库是SQL SERVER 2000,它所在的Dell服务器已经运行超过10年了,早已经过了保修服务期,最近几乎每周会出现一次故障,加之5月份另外一台服务器坏了两个硬 ...
- 从零自学Hadoop(09):使用Maven构建Hadoop工程
阅读目录 序 Maven 安装 构建 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Source ...