现在的位置: 首页 > 综合 > 正文

Android服务(Service)全解析(三)–IntentService

2013年08月16日 ⁄ 综合 ⁄ 共 3103字 ⁄ 字号 评论关闭

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

ServiceSubclass如下:

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

	
}

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

 

抱歉!评论已关闭.