Android Service サンプル(Vibrator)

android_vibrate


FragmentからVibratorを実行したが、Activityを終了すると、バイブレーションも終了してしまうため、サービスに処理を移行し動き続けるようにする。

https://github.com/pppiroto/KaigiUtil/tree/8d52606b8d69f2dd689888a186dda50ae530bdff

1.サービス

Fragmentでやっていた処理を、サービスに持ってくる

package info.typea.kaigiutil;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Vibrator;
import android.widget.Toast;

public class SleepDefenderService extends Service {
    private Vibrator vibrator;

    public SleepDefenderService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        vibrator = (Vibrator)this.getSystemService(Context.VIBRATOR_SERVICE);
        if (!vibrator.hasVibrator()) {
            Toast.makeText(this, "no vibrator.", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "start vibe as service", Toast.LENGTH_SHORT).show();
            long pattern[] = {1000, 100};
            int repeatIndex = 0; // 繰り返し開始位置 -1の場合繰り返しなし
            vibrator.vibrate(pattern, repeatIndex);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        vibrator.cancel();
        Toast.makeText(this, "stop vibe as service", Toast.LENGTH_SHORT).show();
    }
}

1.1 AndroidManifest.xml の application エントリーの子要素に、以下を追加

        <service
            android:name=".SleepDefenderService"
            android:enabled="true"
            android:exported="true"></service>

2.Fragment

Vibratorを操作していた、Fragmentを、サービスの呼び出しと停止に変更

package info.typea.kaigiutil;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class SleepDefenderFragment extends Fragment implements View.OnClickListener {
    private static final String ARG_MSG = "message";

    public SleepDefenderFragment() {
    }

    public static SleepDefenderFragment newInstance(String message) {
        SleepDefenderFragment fragment = new SleepDefenderFragment();
        Bundle args = new Bundle();
        args.putString(ARG_MSG, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_sleep_defender, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.btn_vib_start).setOnClickListener(this);
        view.findViewById(R.id.btn_vib_stop).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this.getContext(), SleepDefenderService.class);

        switch(v.getId()) {
            case R.id.btn_vib_start:
                this.getContext().startService(intent);
                break;

            case R.id.btn_vib_stop:
                this.getContext().stopService(intent);
                break;
        }
    }
}

Android Vibrator サンプル

android_vibrate

https://github.com/pppiroto/KaigiUtil/tree/8dd1ca93ff33afa73714eeaafedbd71ef052a52c

Fragment から Vibratorを起動する。

1.レイアウト

バイブレーター開始ボタン、終了ボタンを配置

<FrameLayout 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"
    tools:context="info.typea.kaigiutil.SleepDefenderFragment">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btn_vib_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="START" />

        <Button
            android:id="@+id/btn_vib_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="STOP" />

    </LinearLayout>
</FrameLayout>

2.バイブレーターを開始/停止する

HTC Desire 626 で動作確認をしていのたがうんともすんとも言わない。設定もおかしくなさそうだし、ググっても不具合的な情報もないし、、、

と、30分くらい途方に暮れていたら、省電力モードだと、バイブレーション機能がOFFになるようだ。省電力モードを切ったら動きました。

package info.typea.kaigiutil;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class SleepDefenderFragment extends Fragment implements View.OnClickListener {
    private static final String ARG_MSG = "message";
    private Vibrator vibrator;

    public SleepDefenderFragment() {
    }

    public static SleepDefenderFragment newInstance(String message) {
        SleepDefenderFragment fragment = new SleepDefenderFragment();
        Bundle args = new Bundle();
        args.putString(ARG_MSG, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_sleep_defender, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.btn_vib_start).setOnClickListener(this);
        view.findViewById(R.id.btn_vib_stop).setOnClickListener(this);

        vibrator = (Vibrator)this.getContext().getSystemService(Context.VIBRATOR_SERVICE);
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()) {
            case R.id.btn_vib_start:

                if (!vibrator.hasVibrator()) {
                    Toast.makeText(this.getContext(), "no vibrator.", Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(this.getContext(), "start vibe", Toast.LENGTH_SHORT).show();
                long pattern[] = {1000, 100 };
                int repeatIndex = 0; // 繰り返し開始位置 -1の場合繰り返しなし
                vibrator.vibrate(pattern, repeatIndex);
                break;

            case R.id.btn_vib_stop:

                vibrator.cancel();
                Toast.makeText(this.getContext(), "stop vibe", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

fragment_dynamic

静的に定義した、Fragment を動的に追加するよう、変更する。https://github.com/pppiroto/KaigiUtil/tree/92bb627ae2256e10008f17b501f022a75806c13c

1.Activity

Fragmentを配置するレイアウト

1.1 レイアウト

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="info.typea.kaigiutil.ContentActivity">

    <LinearLayout
        android:id="@+id/content_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

1.2 Fragmentを読み込むActivity

package info.typea.kaigiutil;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

public class ContentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.content_fragment_container,
                    SleepDefenderFragment.newInstance("test message"))
                .addToBackStack(null)
                .commit();
    }
}

2.呼び出されるFragment

2.1 レイアウト

<FrameLayout 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"
    tools:context="info.typea.kaigiutil.SleepDefenderFragment">

    <TextView
        android:id="@+id/txt_message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TEST" />

</FrameLayout>

2.2 Fratment

package info.typea.kaigiutil;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class SleepDefenderFragment extends Fragment {
    private static final String ARG_MSG = "message";

    public SleepDefenderFragment() {
    }

    public static SleepDefenderFragment newInstance(String message) {
        SleepDefenderFragment fragment = new SleepDefenderFragment();
        Bundle args = new Bundle();
        args.putString(ARG_MSG, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_sleep_defender, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView txtMessage = (TextView)view.findViewById(R.id.txt_message);
        txtMessage.setText(getArguments().getString(ARG_MSG));
    }
}