一、Service真的能执行耗时操作吗?
package com.shadow.intentservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
}
上面实现的是在service里面执行一个耗时操作,然后主activity代码:
package com.shadow.intentservicedemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button startServiceBtn;
private Button startIntentServiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServiceBtn = (Button) findViewById(R.id.startService);
startIntentServiceBtn = (Button) findViewById(R.id.startIntentService);
startServiceBtn.setOnClickListener(this);
startIntentServiceBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startService:
startService(new Intent(this, MyService.class));
break;
case R.id.startIntentService:
startService(new Intent(this, MyIntentService.class));
break;
}
}
}
运行界面:
如上所示,会出现ANR异常,因为service的onSartCommand运行于主UI线程,要想在service中执行耗时操作,则需要另起线程进行处理
package com.shadow.intentservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
}
运行上述service则不回引起ANR异常
二、怎样通过不创建新的线程进行耗时操作呢?
我们知道,在android应用种创建线程很容易引起oom异常,因为线程有可能在应用程序退出后还继续运行,这时,我们需要使用android提供的IntentService来处理耗时异常
package com.shadow.intentservicedemo;
import android.app.IntentService;
import android.content.Intent;
public class MyIntentService extends IntentService {
public MyIntentService(String name) {
super(name);
}
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent arg0) {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行程序,不会弹出ANR异常
三、什么是IntentService?
IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程