Android IntentService 的使用
1.service 执行耗时任务的步骤
2.IntentService
(1)介绍
(2)使用方法
(3)优点
(4)在AndroidManifest.xml文件中添加service设置
- <service android:name=".MyIntentService"></service>
(5)java后台代码
- MainActivity主界面
- package com.lucky.test39intentservice;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Button button1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button1=findViewById(R.id.button);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent=new Intent(MainActivity.this,MyIntentService.class);
- startService(intent);//启动service
- }
- });
- }
- }
MyIntentService
- package com.lucky.test39intentservice;
- import android.app.IntentService;
- import android.content.Intent;
- import android.util.Log;
- public class MyIntentService extends IntentService {
- //构造方法
- public MyIntentService() {
- super("");
- }
- //该方法会在service创建时,就执行
- @Override
- public void onCreate() {
- super.onCreate();
- Log.i("<----------->","任务启动");
- }
- //执行耗时任务
- @Override
- protected void onHandleIntent( Intent intent) {
- int count=100;
- while (count>0){
- Log.i("<---------->",count+"");
- count--;
- try {
- Thread.sleep(500); //对线程进行延时,模拟现实中执行的任务
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.i("<---------->","任务结束");
- }
- }
Android IntentService 的使用的更多相关文章
- Android IntentService完全解析 当Service遇到Handler
一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...
- Android IntentService使用
因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用IntentService类实现服务是很好的选择.本经验将通过继承IntentService输出当前时间教大家如何使用Inten ...
- Android IntentService 与Alarm开启任务关闭任务
1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...
- Android IntentService使用介绍以及源码解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...
- Android IntentService的使用和源码分析
引言 Service服务是Android四大组件之一,在Android中有着举足重轻的作用.Service服务是工作的UI线程中,当你的应用需要下载一个文件或者播放音乐等长期处于后台工作而有没有UI界 ...
- Android IntentService
IntentService简要分析 IntentService 继承自 android.app.Service.内部实现极其简单. 首先在 onCreate()中去开启了一个 HandlerThrea ...
- Android IntentService分析
IntentService其实是一个很通用的知识点,最近看了下阿里巴巴Android开发手册,再次记录下 阿里巴巴Android开发手册 [强制]避免在 BroadcastReceiver#onRec ...
- android IntentService和ResultReceiver的异步处理
IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...
- Android IntentService全然解析 当Service遇到Handler
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...
随机推荐
- Init & Deinit & ARC
[Init & Deinit] 1.switf中,init不返回值,只负责初始化成员变量.在init方法中可以初始化常量. 2.默认初始化. 3.Swift provides an autom ...
- Priceless Notes
[Priceless Notes] 1.人类对价格的绝对值没有准确的判断,但是价格或物体的相对值有较准确的判断. 2.物理强度与主观体验的关联成幂曲线.如60瓦的灯会让人觉得亮,但要让人觉得2倍亮的话 ...
- web服务器推送技术
传统模式的 Web 系统以客户端发出请求.服务器端响应的方式工作.不能满足很多现实应用的需求,譬如: 监控系统:后台硬件温度.电压发生变化: 即时通信系统:其它用户登录.发送信息: 即时报价系统:后台 ...
- Makefile 编写规则 - 1
Makefilen内容 1. 显示规则:显示规则说明了,如何生成一个或多个目标.这是由Makefile指出要生成的文件和文件依赖的文件.2. 隐晦规则:基于Makefile的自动推导功能3. 变量的定 ...
- Codeforces 427E Police Patrol
找中间的数,然后从两头取. #include<stdio.h> ; int pos[MAX]; int main() { int n,m,tmp; int i; int pol; long ...
- MySQL事务隔离级别测试实例
https://www.cnblogs.com/huanongying/p/7021555.html MySQL事务隔离级别 事务隔离级别 脏读 不可重复读 幻读 读未提交(read-uncommit ...
- jsp-MySQL连接池
1.将数据库驱动程序的JAR文件放在Tomcat的 common/lib 中: mysql-connector-java-5.1.18-bin.jar 下载地址:https://yunpan.cn/c ...
- Android ActionBar仿微信界面
ActionBar仿微信界面 1.学习了别人的两篇关于ActionBar博客,在结合别人的文章来仿造一下微信的界面: 思路如下:1).利用ActionBar生成界面的头部,在用ActionBar的Ac ...
- Nexus 私有仓库
Nexus3.6和Nexus2.x安装不同,2.x版本需要安装服务,再启动.而3.6版本则更加简单. 步骤如下: jdk环境:1.8 Nexus3.6解压(注意,路径不要带空格及中文),解压后有两个文 ...
- SQL server T-sql语句查询执行顺序
前言 数据库的查询执行,毋庸置疑是程序员必备的技能之一,然而数据库查询执行的过程绚烂多彩,却是很少被人了解,今天我们来深入了解下sql查询的来龙去脉,为查询的性能优化打个基础 这篇博客,摒弃查询优化性 ...