【Android】Activityとサービスサービスサービスのバインド
コアコード
Activity
Service
適用例
Activityファイル
サービスファイル
XMLレイアウトファイル
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();
}
};
}
サービスファイル
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity"
android:background="#fff" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_marginTop="14dp"
android:text=" service" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text=" cervice" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text=" service" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text=" service" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:text=" service" />
</RelativeLayout>