安卓 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.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈,应用程序的进程还会 ...
随机推荐
- C# 多線程&BackgroundWorker概念入門教程
感謝以下各位作者的貢獻~ 百度經驗舉了個例子,很好理解BackgroundWorker的用途(主要是用來啟動後台線程,而不阻塞調用程式的運行),收藏一下 http://jingyan.baidu.c ...
- SharePoint 2013 必备组件之 Windows Server AppFabric 安装错误
1.如下图,在使用SharePoint2013产品准备工具的时候,网上下载安装Windows Server AppFabric的时候,报错,点击完成重启计算机,重新安装依然报错. 2.无奈之下,只有选 ...
- docker中建立私有git服务器[gitlab]
现在使用git的很普遍,在开发内部如何建立个git服务器,本文以gitlab为例,让你分分钟就可以搭好一个环境[docker的威力非同一般] 首先在docker.com找到gitlab的下载源和信息, ...
- HotApp小程序统计之如何接入
1.统计接入留存说明 更详细的说明,可以查看官网的文档 https://weixin.hotapp.cn/document 统计接入流程只需要4步 (1)注册账号 打开http://weixin.h ...
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
- spring-boot 热部署 intellij IDE
1.使用springloadded插件: 如何使用: a.先在ide里面部署好你的service,( mvn spring-boot:run) b.修改代码, c.command+F9(或build- ...
- java中文件的I/O操作
java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...
- js 调试
$(":select[name='start_Month'").each(function(item,i){ console.log(item.name + "" ...
- [Java入门笔记] 面向对象三大特征之:继承
理解什么是继承 首先我们知道,面对对象有三大特征: 封装:解决了数据的安全性问题 继承:解决了代码的重用问题 多态:解决了程序的扩展问题 上一篇博客中,我们了解了一下封装,现在我了再来看看什么是继承. ...
- Python字符串str的方法使用
#!usr/bin/env python# -*-coding:utf-8-*-#字符串通常用双引号或单引号来表示:'123',"abc","字符串"#str字 ...