展会信息港展会大全

Android服务(Service)全解析(三)--IntentService
来源:互联网   发布日期:2016-01-14 09:29:57   浏览:1759次  

导读: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描述: * IntentServic...

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里面处理耗时的任务

*

* IntentService继承自Service但是会在onHandleIntent()中开启新的线程

* 来处理任务,所以不会造成ANR

*/

public class MainActivity extends Activity {

private Button mStartServiceButton;

private Button mStartIntentServiceButton;

@Override

protected 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() {

@Override

public 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() {

@Override

public void onClick(View arg0) {

Intent intent=new Intent(MainActivity.this, IntentServiceSubclass.class);

startService(intent);

}

});

}

}

package cc.testservice3;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

public class ServiceSubclass extends Service {

@Override

public IBinder onBind(Intent arg0) {

return null;

}

public void onCreate() {

System.out.println("---> Service onCreate()");

}

@Override

public void onStart(Intent intent, int startId) {

super.onStart(intent, startId);

System.out.println("---> Service onStart()");

}

@Override

public 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);

}

@Override

public void onDestroy() {

super.onDestroy();

System.out.println("---> Service onDestroy()");

}

}

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("");

}

@Override

protected void onHandleIntent(Intent intent) {

//模拟耗时的操作

try {

Thread.sleep(30*1000);

} catch (Exception e) {

e.printStackTrace();

}

}

}

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

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港