android IntentService理解

上礼拜去面试时,问到IntentService,妈呀,我平时就只用过Service,直接傻眼了,胡乱说了一通,后面回来脑补了一下。

首先提下Service,Service是android四大组件之一,当有一些操作需要在后台执行,这时我们就想到它了。我们一般会在service里run子线程,需要自己new一个thread。而IntentService继承了Service,说下它的优点吧!
1. 因为IntentService用到了Handler机制,帮我们省去了手动开线程的麻烦.
2. 任务完成后,也会自动destroy掉.
3. 而且还能在里面处理多线程.

现在来说说它的用法吧~

  public class IntentServiceDemo extends IntentService {  

    public IntentServiceDemo() {  
        //必须实现父类的构造方法  
        super("IntentServiceDemo");  
    }  

    @Override  
    public IBinder onBind(Intent intent) {  
        System.out.println("onBind");  
        return super.onBind(intent);  
    }  


    @Override  
    public void onCreate() {  
        System.out.println("onCreate");  
        super.onCreate();  
    }  

    @Override  
    public void onStart(Intent intent, int startId) {  
        System.out.println("onStart");  
        super.onStart(intent, startId);  
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        System.out.println("onStartCommand");  
        return super.onStartCommand(intent, flags, startId);  
    }  

    @Override  
    public void setIntentRedelivery(boolean enabled) {  
        super.setIntentRedelivery(enabled);  
        System.out.println("setIntentRedelivery");  
    }  

    @Override  
    protected void onHandleIntent(Intent intent) {  
        //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务  
        String action = intent.getExtras().getString("param");  
        if (action.equals("action1")) {  
            System.out.println("action1");  
        }else if (action.equals("action2")) {  
            System.out.println("action2");  
        } 
        try {  
            Thread.sleep(2000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  

    @Override  
    public void onDestroy() {  
        System.out.println("onDestroy");  
        super.onDestroy();  
    } 
}

然后在activity中去调用这个IntentService

public class TestActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个  
        //action1  
        Intent startServiceIntent = new Intent(this,IntentServiceDemo.class);  
        Bundle bundle = new Bundle();  
        bundle.putString("param", "action1");  
        startServiceIntent.putExtras(bundle);  
        startService(startServiceIntent);  

        //action2  
        Intent startServiceIntent2 = new Intent(this,IntentServiceDemo.class);  
        Bundle bundle2 = new Bundle();  
        bundle2.putString("param", "action2");  
        startServiceIntent2.putExtras(bundle2);  
        startService(startServiceIntent2);  
    }  
}

最后别忘了在manifest对IntentServiceDemo进行注册

<service android:name=".IntentServiceDemo">  
        service>

将log打印出来,可以知道,IntentServiceDemo中的onCreate()方法只调用了一次,而onStart(),onStartCommand()调用了两次。说明我们虽然new了两个IntentServiceDemo,但是只有一个实例,可以把它理解为一个单例模式,并且当两个任务执行完后,调用了ondestroy方法~

你可能感兴趣的:(android)