org.interactiverobotics.headset_launcher.MediaButtonMonitorService.java Source code

Java tutorial

Introduction

Here is the source code for org.interactiverobotics.headset_launcher.MediaButtonMonitorService.java

Source

/*
 * MediaButtonMonitorService.java
 *
 * Copyright (C) 2015,2016  Pavel Prokhorov (pavelvpster@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package org.interactiverobotics.headset_launcher;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;
import android.view.KeyEvent;

/**
 *    .
 *
 * @author pavelvpster
 *
 */
public final class MediaButtonMonitorService extends Service {

    /**
     *  ? ? logcat  ? ??.
     *
     */
    private static final String TAG = "service";

    /**
     *    .
     *
     */
    private ComponentName mMediaButtonReceiver = null;

    //    private PendingIntent mPendingIntent = null;

    /**
     * ???.
     *
     */
    private MediaSessionCompat mMediaSession = null;

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {

        Log.d(TAG, "Create");

        //   ?   

        mMediaButtonReceiver = new ComponentName(this, MediaButtonReceiver.class);

        // ,  ?   API 18+
        //
        //        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        //
        //        mediaButtonIntent.setComponent(mMediaButtonReceiver);
        //
        //        mPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);

        mMediaSession = new MediaSessionCompat(this, TAG, mMediaButtonReceiver, /* pendingIntent */ null);

        mMediaSession.setCallback(new MediaSessionCompat.Callback() {

            @Override
            public boolean onMediaButtonEvent(Intent mediaButtonEvent) {

                if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonEvent.getAction())) {

                    final KeyEvent keyEvent = (KeyEvent) mediaButtonEvent
                            .getParcelableExtra(Intent.EXTRA_KEY_EVENT);

                    if (KeyEvent.KEYCODE_HEADSETHOOK == keyEvent.getKeyCode()) {

                        if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) {

                            Log.d(TAG, "Media button down!");

                            // ? 

                            startService(new Intent(MediaButtonMonitorService.this, LauncherService.class));
                        }

                    } else {

                        Log.e(TAG, "Unknown keycode: " + keyEvent.getKeyCode());
                    }

                } else {

                    Log.e(TAG, "Unknown intent: " + mediaButtonEvent.getAction());
                }

                return true;
            }

        });

        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);

        mMediaSession.setActive(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d(TAG, "Start");

        return START_STICKY;
    }

}