Android-Activity与服务Service绑定
核心代码Activity
public class MusicActivity extends Activity implements OnClickListener{ @Override public void onClick(View v) {connection(); } private void connection() { Intent intent = new Intent("david.bindService"); bindService(intent, sc, Context.BIND_AUTO_CREATE); } //在客户端覆写onServiceConnected方法,当服务绑定成功会调用此回调函数 private ServiceConnection sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyBinder binder = (MyBinder)service; //通过IBinder获取Service musicService = binder.getService(); if(musicService != null){ musicService.play(); } } }; } }
Service
public class MusicService extends Service { //需用内部类继承Binder,并定义方法获取Service对象 private final IBinder binder = new MyBinder(); public class MyBinder extends Binder { BindMusicService getService() { return BindMusicService.this; } } @Override public IBinder onBind(Intent intent) {//当客户端调用bindService()方法时调用此函数 return binder; } @Override public void onCreate() { } @Override public void onDestroy() { } }
应用范例
Activity文件
package com.app.myservice; import com.app.myservice.service.MyService; import com.app.myservice.service.MyService.MyBind; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ServiceDemo03_Bind extends Activity implements OnClickListener{ private Button startservice,stopservice,binderservice,unbinderservice,getService; private boolean mIsBound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); startservice = (Button) findViewById(R.id.button1); stopservice = (Button) findViewById(R.id.button2); binderservice = (Button) findViewById(R.id.button3); unbinderservice = (Button) findViewById(R.id.button4); getService = (Button) findViewById(R.id.button5); startservice.setOnClickListener(this); stopservice.setOnClickListener(this); binderservice.setOnClickListener(this); unbinderservice.setOnClickListener(this); getService.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(ServiceDemo03_Bind.this,MyService.class); switch (v.getId()) { case R.id.button1: startService(intent); break; case R.id.button2: stopService(intent); break; case R.id.button3: //绑定Service //BIND_AUTO_CREATE表示自动创建 bindService(intent, connection, BIND_AUTO_CREATE); mIsBound = true; break; case R.id.button4: //解除绑定Service if (mIsBound == true) { unbindService(connection); } mIsBound = false; break; case R.id.button5: //提示框 Toast.makeText(ServiceDemo03_Bind.this, "当前Service的值为"+mService.getIndex(), 1000).show(); break; default: break; } } private MyService mService = new MyService(); ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Toast.makeText(ServiceDemo03_Bind.this, "这里是:onServiceDisconnected", 1000).show(); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub Toast.makeText(ServiceDemo03_Bind.this, "这里是:onServiceConnected", 1000).show(); System.out.println("onServiceConnected"); MyService.MyBind myBind = (MyBind) service; mService = myBind.getMyService(); } }; }
Service文件
package com.app.myservice.service; import java.util.Timer; import java.util.TimerTask; import android.R.integer; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service{ private Timer timer; private TimerTask task; private int index=0; Context context; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mBind; } private MyBind mBind = new MyBind(); public class MyBind extends Binder { public MyService getMyService() { return MyService.this; } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); context = getApplicationContext(); Toast.makeText(context, "这里是:onCreate", 1000).show(); System.out.println("onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("onStartCommand"); Toast.makeText(context, "这里是:onStartCommand", 1000).show(); startTimer(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("onDestroy"); Toast.makeText(context, "这里是:onDestroy", 1000).show(); super.onDestroy(); stopTimer(); } public void startTimer() { timer = new Timer(); task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub index++; System.out.println(index); } }; // 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行 timer.schedule(task, 1000, 1000); } public void stopTimer() { timer.cancel();//取消此计时器任务 } public int getIndex() { return index; } }
XML布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:padding补充:移动开发 , Android ,