码迷,mamicode.com
首页 > 其他好文 > 详细

测试Service

时间:2015-03-16 09:54:15      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:android   java   基础   测试      

<strong><span style="font-size:18px;">自定义Service:</span></strong>
/**
 * Created by liuwang on 15-3-15.
 */
public class MyService extends Service {

    private MBinder mBinder = new MBinder();

    public MyService() {
        super();
        System.out.println("MyService---");
    }

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

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

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

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

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("onUnbind---");
        return super.onUnbind(intent);
    }

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



    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        System.out.println("onRebind---");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        System.out.println("onTaskRemoved---");
    }

    @Override
    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
        super.dump(fd, writer, args);
        System.out.println("dump---");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        System.out.println("onTrimMemory---");
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        System.out.println("onConfigurationChanged---");
    }

    class MBinder extends Binder {

    }

}
Activity:
public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    private Button start;
    private Button finish;
    private Button onbind;
    private Button unbind;
    private Intent intent;
    private ServiceConnection sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button) findViewById(R.id.start);
        finish = (Button) findViewById(R.id.finish);
        onbind = (Button) findViewById(R.id.onbind);
        unbind = (Button) findViewById(R.id.unbind);

        start.setOnClickListener(this);
        finish.setOnClickListener(this);
        onbind.setOnClickListener(this);
        unbind.setOnClickListener(this);

        intent = new Intent();
        intent.setClass(this,MyService.class);

        sc = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                System.out.println("连接成功");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                System.out.println("连接失败");
            }
        };

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                startService(intent);
                break;
            case R.id.finish:
                stopService(intent);
                break;
            case R.id.onbind:
                bindService(intent,sc,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(sc);

                break;
        }
    }
}

测试1:

当点击启动Service时的流程:

技术分享

在点击绑定:

技术分享

点击解绑:

技术分享

最后点击关闭服务:

技术分享


测试2:

点击绑定:

技术分享

点击启动:

技术分享

点击关闭服务:

无输出。

点击解除绑定:

技术分享


总结:

当我进行启动服务和绑定服务后,如果只是关闭服务或解绑服务并不会将service杀死。

当我进行启动服务后在绑定,并不会重复创建service。

先启动在绑定的流程:构造方法->oncreate->onstartcommand->onstart->onbind->onserviceconnection

先绑定service在启动service流程:构造方法->oncreate->onbind->onserviceconnection->onstartcommand->onstart

当我重复启动service只会重复调用startcommand和start方法,不会重复调用oncreate

当我进行多次绑定时并不会重复创建多个service

当我绑定service并且退出activity不解绑service时,service会自动被关闭,并且logcat会报错

我可以利用绑定service后启动service然后退出activity解绑service,这样service还不会被kill,再次进入activity我们可以通过关闭service使service关闭。


测试Service

标签:android   java   基础   测试      

原文地址:http://blog.csdn.net/u012706758/article/details/44300407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!