Skip to content

Instantly share code, notes, and snippets.

@typosone
Last active August 29, 2015 14:11
Show Gist options
  • Save typosone/be07fe394d7775d8f16f to your computer and use it in GitHub Desktop.
Save typosone/be07fe394d7775d8f16f to your computer and use it in GitHub Desktop.
Android の Service のさんぷる
<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=".MainActivity">
<Button
android:id="@+id/start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" />
<Button
android:id="@+id/stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/start_service"
android:text="Stop Service" />
<Button
android:id="@+id/bind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/stop_service"
android:text="Bind Service" />
<Button
android:id="@+id/unbind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bind_service"
android:text="Unbind Service" />
<Button
android:id="@+id/start_vibrator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/unbind_service"
android:enabled="false"
android:text="ヴァイブレーションスタート" />
<Button
android:id="@+id/stop_vibrator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/unbind_service"
android:layout_toRightOf="@id/start_vibrator"
android:enabled="false"
android:text="ヴァイブレーションストップ" />
</RelativeLayout>
package jp.ac.it_college.std.nakasone.servicetest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity extends Activity {
private MyService service;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.LocalBinder binder = (MyService.LocalBinder) service;
MainActivity.this.service = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
startVibrator();
break;
case R.id.stop_service:
stopVibrator();
break;
case R.id.bind_service:
bind();
findViewById(R.id.start_vibrator).setEnabled(true);
findViewById(R.id.stop_vibrator).setEnabled(true);
break;
case R.id.unbind_service:
unbind();
findViewById(R.id.start_vibrator).setEnabled(false);
findViewById(R.id.stop_vibrator).setEnabled(false);
break;
case R.id.start_vibrator:
service.startVibrator();
break;
case R.id.stop_vibrator:
service.stopVibrator();
break;
}
}
};
findViewById(R.id.start_service).setOnClickListener(listener);
findViewById(R.id.stop_service).setOnClickListener(listener);
findViewById(R.id.bind_service).setOnClickListener(listener);
findViewById(R.id.unbind_service).setOnClickListener(listener);
findViewById(R.id.start_vibrator).setOnClickListener(listener);
findViewById(R.id.stop_vibrator).setOnClickListener(listener);
}
private void startVibrator() {
startService(new Intent(this, MyService.class));
}
private void stopVibrator() {
stopService(new Intent(this, MyService.class));
}
private void bind() {
Intent intent = new Intent(this, MyService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
private void unbind() {
unbindService(serviceConnection);
}
}
package jp.ac.it_college.std.nakasone.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private static final long[] PATTERN = {50, 100, 50, 200, 50, 300, 50, 200, 50, 100};
private Vibrator vibrator;
private IBinder binder = new LocalBinder();
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
initVibrator();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startVibrator();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
stopVibrator();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private void initVibrator() {
try {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
} catch (ClassCastException e) {
Log.wtf(TAG, e);
throw e;
}
}
public void startVibrator() {
if (vibrator.hasVibrator()) {
vibrator.vibrate(PATTERN, 0);
}
}
public void stopVibrator() {
if (vibrator.hasVibrator()) {
vibrator.cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment