服务(Service)全解析(三)--IntentService
来源:互联网 发布:百度云显示网络不给力 编辑:程序博客网 时间:2024/04/28 05:36
MainActivity如下:
package cc.testservice3;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * Demo描述: * IntentService的使用 * * Demo详情: * 在此处使用Service和IntentService模拟耗时任务. * 那么Service会出现ANR错误,IntentService则不会 * * 原因说明: * 1 Service不是专门启动的一条单独进程,Service与它所在的应用位于同一进程中 * 2 Service也不是一条新的线程,所以不能在Service里面处理耗时的任务。所以,可以在service中开启子线程来处理耗时的任务 * * IntentService继承自Service但是会在onHandleIntent()中开启新的线程 * 来处理任务,所以不会造成ANR * * 总结IntentService的两大特点: * 1 会开启一个新的线程执行异步耗时任务 * 2 IntentService在执行完耗时任务后会自动停止该Service * 不用我们以前那样stopService()或者unbindService(). */public class MainActivity extends Activity { private Button mStartServiceButton; private Button mStartIntentServiceButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){ //利用Service操作耗时任务mStartServiceButton=(Button) findViewById(R.id.startServiceButton);mStartServiceButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent=new Intent(MainActivity.this, ServiceSubclass.class);startService(intent);}});//利用IntentService操作耗时任务mStartIntentServiceButton=(Button) findViewById(R.id.startIntentServiceButton);mStartIntentServiceButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent=new Intent(MainActivity.this, IntentServiceSubclass.class);startService(intent);}});}}
ServiceSubclass如下:
package cc.testservice3;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class ServiceSubclass extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}public void onCreate() {System.out.println("---> Service onCreate()");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);System.out.println("---> Service onStart()");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("---> Service onStartCommand()");//模拟耗时的操作try {Thread.sleep(30*1000);} catch (Exception e) {e.printStackTrace();}return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("---> Service onDestroy()");}}
IntentServiceSubclass如下:
package cc.testservice3;import android.app.IntentService;import android.content.Intent;/** * 注意事项: * 在继承自IntentService时Eclipse会提示生成一个带String参数的 * 构造方法.我们按照该提示生成构造方法后,运行时会报错: * java.lang.InstantiationException * * 解决办法: * 删除该提示生成的构造方法,写一个空参数的构造方法. * 但在该空参数构造方法里执行super(""); * */public class IntentServiceSubclass extends IntentService {public IntentServiceSubclass() {super("");}@Overrideprotected void onHandleIntent(Intent intent) {//模拟耗时的操作try {Thread.sleep(30*1000);} catch (Exception e) {e.printStackTrace();}}}
main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" > <Button android:id="@+id/startServiceButton" android:layout_width="200dip" android:layout_height="150dip" android:text="启动Service" /> <Button android:id="@+id/startIntentServiceButton" android:layout_width="200dip" android:layout_height="150dip" android:text="启动IntentService" /></LinearLayout>
- 服务(Service)全解析(三)--IntentService
- android后台服务service全解析(中)--IntentService与Notification前台通知
- 服务(Service)全解析(五)--前台Service
- Android-服务Service(3)-IntentService
- 服务(Service)全解析(四)--AIDL
- 服务(Service)全解析(二)--bindService
- 服务(Service)全解析(一)--startService
- Android Service详解(三)---IntentService
- android后台服务service全解析(下)--service远程通信
- Android Service完全解析(Service,IntentService)
- Android Service生命周期,本地服务远程服务,前台服务后台服务,启动方式,与IntentService区别全面解析
- Tomcat源码解析(三):service服务组件
- 深入理解Service(三)——前台服务和IntentService
- android服务Service(上)- IntentService
- Android Service串行服务:IntentService(系列3)
- Service知识点全解析(二)——远程服务
- Service的子类IntentService(三)
- Android Service(三) IntentService详解
- 《大话数据结构》读书笔记之快速排序源码及其优化
- 最近在给同事们分享一些JAVA Web方向的技术,全程录屏,分享给有需要的朋友们,不喜勿喷!
- iOS学习之 plist文件的读写
- 机器学习——深度学习(Deep Learning)
- hdu 1003(DP)
- 服务(Service)全解析(三)--IntentService
- 哈希函数简介
- opencv 图片中的 人脸检测
- 1010. Zipper
- 全站乱码 我的经典错误。。。。。。
- hdu4680 About set -------splay
- poj 2253 Frogger
- bitmap的使用
- 服务(Service)全解析(二)--bindService