Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法
绑定代码如下
Activity类代码:
[java] view plaincopy
- <span style="font-size:16px;">package com.fox.Activity;
- import com.fox.Activity.service.Service1;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class Activity1 extends Activity {
- private Button btn1 = null;
- private static String LOG="mp3";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn1 = (Button) findViewById(R.id.button1);
- btn1.setOnClickListener(new btn1ClickListener());
- //开始绑定
- Intent intent = new Intent(Activity1.this,Service1.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
- private Service1 myservice = null;//绑定的service对象
- //连接对象,重写OnserviceDisconnected和OnserviceConnected方法
- public ServiceConnection conn= new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.i(LOG, "onServiceDisconnected>>>>>>>>");
- myservice = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.i(LOG, "onServiceConnected>>>>>>>>");
- myservice = ((Service1.MyBinder)service).getService();
- Log.i(LOG, myservice+">>>>>>>>");
- }
- };
- class btn1ClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- unbindService(conn);
- }
- }
- }</span>
Service类代码:
[java] view plaincopy
- <span style="font-size:16px;">package com.fox.Activity.service;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
- public class Service1 extends Service{
- private final IBinder binder = new MyBinder();
- private static final String LOG="mp3";
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(LOG, "onBind............");
- return binder;
- }
- /**
- * 该类是获得Service对象
- * @author Administrator
- *
- */
- public class MyBinder extends Binder{
- public Service1 getService(){
- return Service1.this;
- }
- }
- @Override
- public void onCreate() {
- Log.i(LOG, "oncreate............");
- super.onCreate();
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Log.i(LOG, "onstart............");
- super.onStart(intent, startId);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i(LOG, "onstartcommand............");
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- Log.i(LOG, "ondestory............");
- super.onDestroy();
- }
- }
- </span>
开始绑定调用方法A.bindService()--->S.onCreate--->S.onBind---->>A.onServiceConnected绑定成功,并获得Service对象
结束绑定按钮的监听事件-->>unbindService(conn)关闭连接对象-->>S.destory()销毁该service
注:结束绑定时是不会调用onServiceDisconnected()方法的;
http://blog.csdn.net/huqingwei0824/article/details/6869622
时间: 2024-10-14 09:50:48