您的位置:首页 > 其它

IntentService的使用介绍

2016-04-29 11:10 225 查看


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" >

<TextView
android: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" />

<Button
android: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="上传" />

<Button
android: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 {

@Override
protected 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");
}// 又不要返回 就是标记的作用

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("oncreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("onstartCommant");
return super.onStartCommand(intent, flags, startId);
}

@Override
protected 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;
}
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestory");
}
// spring :strus javabena 系统调用空的构造方法
}


专启一个线程来处理耗时操作,而普通service却不会。

有两点需要注意

1。toast 并有显示

2。SystemClock.sleep(2000);睡眠两秒是为了每批次的请求队列不被销毁!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: