码迷,mamicode.com
首页 > 移动开发 > 详细

Android---43---Service之IntentService

时间:2015-05-21 17:28:50      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:android   intentservice   




首先说一下Service本身存在的两个问题:

1.Service不会专门启动一条新的线程,Service与它所在应用位于同一个进程中。

2.Service也不是专门一条新的线程,所以不应该在Service中处理耗时的任务。



IntentService正好弥补了这两个不足:

IntentService可以处理异步请求的Service。

IntentService将会使用队列来管理请求Intent,每当客户端代码通过Intent请求启动IntentService时,IntentService
会将该Intent加入到队列中,然后开启一条新的worker线程来处理该Intent。在完成的时候会自动停止,不用手动去停止它。


IntentService的特点:
IntentService会创建单独的worker线程来处理所有的Intent请求
IntentService会创建单独的worker线程来处理onHandleIntent方法实现的代码,因此开发者不需要处理多线程问题
当所有的请求处理完成后,IntentService会自动停止,无需调用stopSelf方法来停止该Service
为Service的onBind方法提供了默认实现,默认该方法返回null
为Service的onStartCommand方法提供了默认实现,该实现会将Intent添加到队列中。


所以在继承IntentService时,不需要onBind onStartCommand 方法,只要实现onHandleIntent方法即可




public class IntentServiceTest extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void startService(View source)
	{
		// 创建需要启动的Service的Intent
		Intent intent = new Intent(this, MyService.class);
		// 启动Service
		startService(intent);
	}

	public void startIntentService(View source)
	{
		// 创建需要启动的IntentService的Intent
		Intent intent = new Intent(this, MyIntentService.class);
		// 启动IntentService
		startService(intent);
	}
}

上面Activity的两个时间处理方法中分别启动MyService与MyIntentService。



MyService.java:


public class MyService extends Service
{
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		// 该方法内执行耗时任务可能导致ANR(Application Not Responding)异常
		long endTime = System.currentTimeMillis() + 20 * 1000;
		System.out.println("onStart");
		while (System.currentTimeMillis() < endTime)
		{
			synchronized (this)
			{
				try
				{
					wait(endTime - System.currentTimeMillis());
				}
				catch (Exception e)
				{
				}
			}
		}
		Toast.makeText(getApplicationContext(), "耗时任务执行完成", 1).show();
		System.out.println("---耗时任务执行完成---");
		return START_STICKY;
	}
}


MyIntentService.java:


public class MyIntentService extends IntentService
{
	public MyIntentService()
	{
		super("MyIntentService");
	}

	// IntentService会使用单独的线程来执行该方法的代码
	@Override
	protected void onHandleIntent(Intent intent)
	{
		// 该方法内可以执行任何耗时任务,比如下载文件等,此处只是让线程暂停20秒
		long endTime = System.currentTimeMillis() + 20 * 1000;
		System.out.println("onStart");
		while (System.currentTimeMillis() < endTime)
		{
			synchronized (this)
			{
				try
				{
					wait(endTime - System.currentTimeMillis());
				}
				catch (Exception e)
				{
				}
			}
		}
		System.out.println("---耗时任务执行完成---");
		Toast.makeText(getApplicationContext(), "耗时任务执行完成", 0).show();
	}
}





Android---43---Service之IntentService

标签:android   intentservice   

原文地址:http://blog.csdn.net/u013476556/article/details/45893291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!