IntentService的使用介绍

 2023-09-05 阅读 70 评论 0

摘要:IntentService简介 一,IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题: Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中; Service也不是专门一条新线程,因

效果

IntentService简介

一,IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题:
Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中;
Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务;
二、IntentService特征
会创建独立的worker线程来处理所有的Intent请求;
会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
为Service的onBind()提供默认实现,返回null;
为Service的onStartCommand提供默认实现,将请求Intent添加到队列中;
布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.intentservice.MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="17dp"android:text="意图服务"android:textSize="30dp" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/textView1"android:layout_centerHorizontal="true"android:onClick="upload"android:layout_marginTop="38dp"android:text="上传" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button1"android:layout_centerVertical="true"android:text="下载"android:onClick="download" /></RelativeLayout>

activity的代码

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}// 上传public void upload(View v) {Intent intent=new Intent(this, FileService.class);intent.putExtra("cmd", 1);startService(intent);}public void download(View v){Intent intent=new Intent(this, FileService.class);intent.putExtra("cmd", 2);startService(intent);   }}

再来是重点 FileService的代码

public class FileService extends IntentService {public FileService() {// name是改service的一个标记,一般不用 系统内部调用会有一个映射super("myFileService");}// 又不要返回 就是标记的作用@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("oncreate");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("onstartCommant");return super.onStartCommand(intent, flags, startId);}@Overrideprotected void onHandleIntent(Intent intent) {// 在子线程运行,耗时操作System.out.println("onHandleIntent");int cmd = intent.getIntExtra("cmd", 0);switch (cmd) {case 1:System.out.println("onHandleIntent+上传");Toast.makeText(getApplicationContext(), "上传", Toast.LENGTH_SHORT).show();SystemClock.sleep(2000);break;case 2:System.out.println("onHandleIntent+下载");Toast.makeText(getApplicationContext(), "下载", Toast.LENGTH_SHORT).show();break;default:break;}}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("onDestory");}// spring :strus javabena 系统调用空的构造方法
}

专启一个线程来处理耗时操作,而普通service却不会。
有两点需要注意
1。toast 并有显示
2。SystemClock.sleep(2000);睡眠两秒是为了每批次的请求队列不被销毁!

转载于:https://www.cnblogs.com/Tesi1a/p/7624146.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/1153.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息