服务(Service)全解析(三)--IntentService

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值