源碼解析
IntentService 是一個繼承Service 的服務(wù)。在內(nèi)部封裝一個叫ServiceHandler的Handler類扁远,在handleMessage中處理數(shù)據(jù)時調(diào)用onHandleIntent(),而onHandleIntent是一個抽象方法似芝,由子類去實現(xiàn)蝶棋。
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);
使用解析
首先創(chuàng)建一個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 這個是在子線程中運(yùn)行的熄赡,所以可進(jìn)行耗時操作。
完整實例代碼
TestIntentService.java
public class TestIntentService extends IntentService {
private boolean isRunning = true;
private int count = 0;
//用一個本地廣播發(fā)送數(shù)據(jù)
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;
}
}
}
}