1. 程式人生 > >Service 和 IntentService的區別;

Service 和 IntentService的區別;

Srevice不是在子執行緒,在Srevice中做耗時操作一樣ANR,然後我們就會用到IntentService,IntentSrevice不但擅長做耗時操作,還有一個特點,用完即走;

在Srevice中做耗時輪詢操作,使用Handler:


public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    private Handler mHandler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what){
                case HANDLERSIGN:
                    Log.i(TAG, "dispatchMessage: "+args+(++num));
                    mHandler.sendEmptyMessageDelayed(HANDLERSIGN,HANDLERTIME);
                    if (num == 5){
                        AlertDialog.Builder builder = new AlertDialog.Builder(MyService.this);
                        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MyService.this.stopSelf();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.setMessage("我的計數"+num);
                        dialog.setTitle("提示");
                        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                        dialog.show();
                    }
                    break;
            }
        }
    };

    private final String TAG = "ccb";
    private String args;
    private int num;
    private final int HANDLERSIGN = 10;
    private final int HANDLERTIME = 2010;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       args = intent.getStringExtra("args");
        initData();
        return super.onStartCommand(intent, flags, startId);
    }

    private void initData() {
        mHandler.sendEmptyMessageDelayed(HANDLERSIGN,HANDLERTIME);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: 啊,ByKill");
        mHandler.removeCallbacksAndMessages(null);
    }
}

在IntentSrevice中做耗時輪詢操作,可以任性到這種程度:


public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private final String TAG = "ccb";
    private String args;
    private int num;
    @Override
    protected void onHandleIntent(Intent intent) {
        args = intent.getStringExtra("args");
        initData();
    }

    private void initData() {
        for (int i = 0; i < 30; i++) {
            try {
                Thread.sleep(3000);
                Log.i(TAG, "dispatchMessage: "+args+(++num));
                if (num == 5) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MyIntentService.this);
                    AlertDialog dialog = builder.setPositiveButton("確定", null).create();
                    dialog.setMessage("我的計數" + num);
                    dialog.setTitle("我是服務");
                    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    dialog.show();
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: MyIntentService");
    }
}

IntentSrevice中的onHandlerIntent()方法走完就會銷燬掉自己,立馬走onDestroy()方法;