安卓4大组件之一服务Service

来源:互联网 发布:nginx 配置tomcat跳转 编辑:程序博客网 时间:2024/04/27 17:49

Service作为Android四大组件之一,在每一个应用程序中都扮演着非常重要的角色。它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务。必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持运行状态。(比如听音乐,退出程序英语需要继续播放。数据定期更新都需要服务来实现)

基本用法:
服务是4大组件之一必须在清单文件中注册:

<service android:name="com.example.myservice.Myservice"></service>

新建一个MyService继承自Service,并重写父类的onCreate()、onStartCommand()和onDestroy()方法,如下所示:

public class Myservice extends Service { class MusicController extends Binder {        public void Abb(){            Log.e("e", "连接了绑定服务");        }    }  //绑定服务    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        Log.e("e", "onBind");        return null;    }    //关闭绑定服务    @Override    public boolean onUnbind(Intent intent) {        // TODO Auto-generated method stub        Log.e("e", "onUnbind");        return super.onUnbind(intent);    }    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();        Log.e("e", "onCreate");    }    @Override    public void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        Log.e("e", "onDestroy");    }    //普通服务的(绑定服务不走着个方法)    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        // TODO Auto-generated method stub        Log.e("e", "onStartCommand");        return super.onStartCommand(intent, flags, startId);    }}

MainActivity当中开启服务:

public class MainActivity extends ActionBarActivity implements OnClickListener {    private MyServiceConnextion connextion;    Boolean isBind=false;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button bt=(Button) findViewById(R.id.bt);        Button bt2=(Button) findViewById(R.id.bt2);        connextion = new MyServiceConnextion();        bt.setOnClickListener(this);        bt2.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {        case R.id.bt:            //启动普通服务            /*startService(new Intent(MainActivity.this,Myservice.class));*/            //启动绑定服务            if (!isBind) {                isBind=true;                bindService(new Intent(MainActivity.this,Myservice.class), connextion,BIND_AUTO_CREATE);            }            break;        case R.id.bt2:            //关闭普通服务            /*stopService(new Intent(MainActivity.this,Myservice.class));*/            //关闭绑定服务            if (isBind) {                isBind=false;                unbindService(connextion);            }            break;        default:            break;        }    }    //绑定服务    public class MyServiceConnextion implements ServiceConnection{        @Override        public void onServiceConnected(ComponentName arg0, IBinder arg1) {            // TODO 自动生成的方法存根            Myservice.MusicController m = (Myservice.MusicController)arg1;            m.Abb();            Log.e("e", "我连接了");        }        /*ServiceConnection 的回调方法onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的,         该方法只在Service 被破坏了或者被杀死的时候调用.例如, 系统资源不足, 要关闭一些Services,         刚好连接绑定的 Service 是被关闭者之一,  这个时候onServiceDisconnected() 就会被调用。*/        @Override        public void onServiceDisconnected(ComponentName arg0) {            // TODO 自动生成的方法存根            Log.e("e", "我断开了");        }    }}

参考网址:http://blog.csdn.net/guolin_blog/article/details/11952435

原创粉丝点击