源码解析
IntentService 是一个继承Service 的服务。在内部封装一个叫ServiceHandler的Handler类,在handleMessage中处理数据时调用onHandleIntent(),而onHandleIntent是一个抽象方法,由子类去实现。
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
@WorkerThread
protected abstract void onHandleIntent(Intent intent);
使用解析
首先创建一个Service继承IntentService,并重写onHandleIntent()方法。
@Override
protected void onHandleIntent(@Nullable Intent intent) {
try {
isRunning = true;
count = 0;
while (isRunning){
count++;
if(count >= 100){
isRunning = false;
}
sendThreadProgress(count);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
onHandleIntent 这个是在子线程中运行的,所以可进行耗时操作。
完整实例代码
TestIntentService.java
public class TestIntentService extends IntentService {
private boolean isRunning = true;
private int count = 0;
//用一个本地广播发送数据
private LocalBroadcastManager mLocalBroadcastManager;
public TestIntentService() {
super("TestIntentService");
}
@Override
public void onCreate() {
super.onCreate();
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
try {
isRunning = true;
count = 0;
while (isRunning){
count++;
if(count >= 100){
isRunning = false;
}
sendThreadProgress(count);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void sendThreadProgress(int progress){
Intent intent = new Intent(IntentServiceActivity.ACTION_TYPE_THREAD);
intent.putExtra("progress", progress);
mLocalBroadcastManager.sendBroadcast(intent);
}
}
IntentServiceActivity.java
public class IntentServiceActivity extends BaseActivity {
public static final String ACTION_TYPE_THREAD = "action.type.thread";
private Button mBtn;
private TextView mText;
private ProgressBar mBar;
private LocalBroadcastManager mLocalBroadcastManager;
private MyBroadcastReceiver mBroadcastReceiver;
@Override
protected void loadViewLayout() {
setContentView(R.layout.test_asyn_task_activity);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_TYPE_THREAD);
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
}
@Override
protected void loadIntent() {
}
@Override
protected void bindViews() {
mBtn = (Button) findViewById(R.id.start);
mText = (TextView) findViewById(R.id.text);
mBar = (ProgressBar) findViewById(R.id.pba);
}
@Override
protected void processLogic(Bundle savedInstanceState) {
}
@Override
protected void setListener() {
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(mContext, TestIntentService.class);
startService(startIntent);
}
});
findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(mContext, TestIntentService.class);
stopService(startIntent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
}
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case ACTION_TYPE_THREAD:
int progress = intent.getIntExtra("progress", 0);
mBar.setProgress(progress);
mText.setText(progress + "%");
break;
}
}
}
}