Example usage for android.content Intent ACTION_HEADSET_PLUG

List of usage examples for android.content Intent ACTION_HEADSET_PLUG

Introduction

In this page you can find the example usage for android.content Intent ACTION_HEADSET_PLUG.

Prototype

String ACTION_HEADSET_PLUG

To view the source code for android.content Intent ACTION_HEADSET_PLUG.

Click Source Link

Document

Broadcast Action: Wired Headset plugged in or unplugged.

Usage

From source file:Main.java

public static String getWiredHeadsetUpdateAction() {
    if (Build.VERSION.SDK_INT >= 21) {
        return AudioManager.ACTION_HEADSET_PLUG;
    } else {//from  w w w  .j  ava2s. c o m
        return Intent.ACTION_HEADSET_PLUG;
    }
}

From source file:com.Context.HeadsetWatcher.java

@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
    this.callback = callbackId;
    headsetReceiver = new HeadsetBroadcastReceiver(this);
    PluginResult result = new PluginResult(Status.NO_RESULT);
    this.cordova.getActivity().registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    result.setKeepCallback(true);/*  w  ww.  ja v a  2  s  .  c o m*/
    return result;
}

From source file:org.FrancoisDescamps.CatPlayer.MusicService.java

public void playMusic(final String pathToMusic) {
    //register receiver
    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadphonesUnplugReceiver receiver = new HeadphonesUnplugReceiver();
    registerReceiver(receiver, receiverFilter);

    if (mp != null) {
        mp.stop();// w  w  w . jav  a  2  s.  c o m
        mp = null;
    }

    try {
        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setDataSource(pathToMusic);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        /*NOP*/
    }

    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            switch (repeat) {
            case 0: // Lecture en boucle dsactive
                /* NOP */
                break;
            case 1: // Lecture en boucle d'une seule musique
                playMusic(pathToMusic);
                break;
            case 2: // Lecture en boucle de toutes les musiques
                if (positionOfInitialMusic + cpt + 1 == queu.size()) {
                    cpt = 0;
                    positionOfInitialMusic = 0;
                } else {
                    cpt++;
                }
                playMusic(queu.get(positionOfInitialMusic + cpt).getPath());
                break;
            }

            currentTitle = properties[0][positionOfInitialMusic + cpt];
            currentAlbum = properties[1][positionOfInitialMusic + cpt];
            currentArtist = properties[2][positionOfInitialMusic + cpt];
            currentPath = properties[3][positionOfInitialMusic + cpt];

            musicAsChanged = true;
            MainActivity.notifyOtherMusicStarted();
            buildNotification(true);

            //save "cpt"
            editor = preferences.edit();
            editor.putInt("cpt", cpt);

            if (Build.VERSION.SDK_INT < 9) {
                editor.commit();
            } else {
                editor.apply();
            }
        }
    });
}

From source file:org.chirpradio.mobile.PlaybackService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Debug.log(this, "Received start id " + startId + ": " + intent);
    registerReceiver(headphoneReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    Debug.log(this, "Starting get playlist task");
    updatePlaylist = true;//  w w  w.j  a  v a 2s. co  m
    handler = new Handler();
    handler.post(mUpdateTask);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

From source file:ru.org.sevn.audiobookplayer.AppService.java

@Override
public void onCreate() {
    defaultIcon = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
    defaultIcon = Util.getScaledImage(defaultIcon, ICON_SIZE, ICON_SIZE, Util.getImageFactor(getResources()));

    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    super.onCreate();
    mp3Player.initialize();/*from www . jav a  2s  .c o  m*/
    restoreSettings();
    mp3Player.getMediaPlayer().addChangeStateListener(this);
    mp3Player.getAppSettings().addPropertyChangeListener(this);

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    musicIntentReceiver = new MusicIntentReceiver();
    registerReceiver(musicIntentReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

}

From source file:com.brejza.matt.habmodem.Dsp_service.java

@Override
public IBinder onBind(Intent arg0) {

    if (!isRecording)
        serviceRestart();//from   ww  w.ja v  a2  s  .  c  om
    if (serviceInactiveTimer != null) {
        serviceInactiveTimer.cancel();
        serviceInactiveTimer = null;
        logEvent("Stopping Inactivity Timer", false);
    }

    System.out.println("DEBUG : something bound");

    handler = new Handler();

    //string receiver
    if (headsetReceiver == null)
        headsetReceiver = new HeadsetReceiver();
    IntentFilter intentFilter1 = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headsetReceiver, intentFilter1);

    if (hab_con == null) {
        String call_u = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext())
                .getString("pref_callsign", "USER");

        hab_con = new Habitat_interface(
                PreferenceManager.getDefaultSharedPreferences(this).getString("pref_habitat_server",
                        "habitat.habhub.org"),
                PreferenceManager.getDefaultSharedPreferences(this).getString("pref_habitat_db", "habitat"),
                new Listener(call_u, new Gps_coordinate(50.2, -0.6, 0), false));
        //hab_con.upload_payload_telem(new Telemetry_string("$$ASTRA,12:12:12,5044.11111,-001.00000,1212,34*1234"));   
        hab_con.addGetActiveFlightsTask();
        hab_con.addHabitatRecievedListener(this);
    }
    if (loc_han == null) {
        loc_han = new Location_handler();
        this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    hab_con.device = android.os.Build.BRAND + " " + android.os.Build.MODEL;
    hab_con.device_software = android.os.Build.VERSION.RELEASE;
    hab_con.application = "HAB Modem for Android";
    String vers = "unknown";
    try {
        Context cn = getApplicationContext();
        vers = cn.getPackageManager().getPackageInfo(cn.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        System.out.println("Cannot get version number - " + e.toString());
    }
    hab_con.application_version = vers;

    //System.out.println("Starting audio");
    return mBinder;
}

From source file:re.serialout.MainScreen.java

@Override
public void onResume() {
    super.onResume();
    IntentFilter iff = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    // Put whatever message you want to receive as the action
    this.registerReceiver(this.AudioBroadcastReciver, iff);
}

From source file:the.joevlc.AudioService.java

@Override
public void onCreate() {
    super.onCreate();

    // Get libVLC instance
    try {/*from  w  ww . j  a v  a 2  s.c o m*/
        mLibVLC = LibVLC.getInstance();
    } catch (LibVlcException e) {
        e.printStackTrace();
    }

    Thread.setDefaultUncaughtExceptionHandler(new VlcCrashHandler());

    mCallback = new HashMap<IAudioServiceCallback, Integer>();
    mMediaList = new ArrayList<Media>();
    mPrevious = new Stack<Media>();
    mEventManager = EventManager.getInstance();
    mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(),
            RemoteControlClientReceiver.class.getName());

    // Make sure the audio player will acquire a wake-lock while playing. If we don't do
    // that, the CPU might go to sleep while the song is playing, causing playback to stop.
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(VLCApplication.SLEEP_INTENT);
    registerReceiver(serviceReceiver, filter);

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean stealRemoteControl = pref.getBoolean("steal_remote_control", false);

    if (!Util.isFroyoOrLater() || stealRemoteControl) {
        /* Backward compatibility for API 7 */
        filter = new IntentFilter();
        if (stealRemoteControl)
            filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(Intent.ACTION_MEDIA_BUTTON);
        mRemoteControlClientReceiver = new RemoteControlClientReceiver();
        registerReceiver(mRemoteControlClientReceiver, filter);
    }

    AudioUtil.prepareCacheFolder(this);
}

From source file:org.videolan.vlc.AudioService.java

@Override
public void onCreate() {
    super.onCreate();

    // Get libVLC instance
    try {//from w w w.  ja v a 2  s  . co  m
        mLibVLC = Util.getLibVlcInstance();
    } catch (LibVlcException e) {
        e.printStackTrace();
    }

    Thread.setDefaultUncaughtExceptionHandler(new VlcCrashHandler());

    mCallback = new HashMap<IAudioServiceCallback, Integer>();
    mMediaList = new ArrayList<Media>();
    mPrevious = new Stack<Media>();
    mEventHandler = EventHandler.getInstance();
    mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(),
            RemoteControlClientReceiver.class.getName());

    // Make sure the audio player will acquire a wake-lock while playing. If we don't do
    // that, the CPU might go to sleep while the song is playing, causing playback to stop.
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(ACTION_REMOTE_BACKWARD);
    filter.addAction(ACTION_REMOTE_PLAYPAUSE);
    filter.addAction(ACTION_REMOTE_PLAY);
    filter.addAction(ACTION_REMOTE_PAUSE);
    filter.addAction(ACTION_REMOTE_STOP);
    filter.addAction(ACTION_REMOTE_FORWARD);
    filter.addAction(ACTION_REMOTE_LAST_PLAYLIST);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(VLCApplication.SLEEP_INTENT);
    registerReceiver(serviceReceiver, filter);

    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    boolean stealRemoteControl = pref.getBoolean("enable_steal_remote_control", false);

    if (!Util.isFroyoOrLater() || stealRemoteControl) {
        /* Backward compatibility for API 7 */
        filter = new IntentFilter();
        if (stealRemoteControl)
            filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(Intent.ACTION_MEDIA_BUTTON);
        mRemoteControlClientReceiver = new RemoteControlClientReceiver();
        registerReceiver(mRemoteControlClientReceiver, filter);
    }

    AudioUtil.prepareCacheFolder(this);
}

From source file:re.serialout.MainScreen.java

private void receivedBroadcast(Intent i) {
    if (am.isWiredHeadsetOn()) {
        System.out.println("HEADSET DETECTED!!!");
    } else {/*from  ww w.  j  a v  a  2  s  .c om*/
        System.out.println("NO HEADSET!!!");
    }
    if (!idSet) {
        if (i.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = i.getIntExtra("state", -1);
            switch (state) {
            case 0:
                deviceConnected = false;
                break;
            case 1:
                break;
            default:
            }
        }
        return;
    }

    //final Animation scale = AnimationUtils.loadAnimation(this, R.anim.scale);
    if (i.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        final Button activate = (Button) findViewById(R.id.seqButton);
        final TextView mainText = (TextView) findViewById(R.id.mainText);
        activate.setText("Activate Device");
        int state = i.getIntExtra("state", -1);
        switch (state) {
        case 0:
            deviceConnected = false;
            if (activate.getVisibility() == View.VISIBLE) {
                //                        longOperation.cancel(true);
                playSine.stop();
                System.out.println("Playsine told to stop");
                mainText.setText("\nPlease Plug in pH Sensor\n");
            }
            activate.setVisibility(View.GONE);
            break;
        case 1:
            deviceConnected = true;

            //                    if (idSet) {
            playSine.start();
            System.out.println("Play Sine told to start");
            if (!commandInterface.setUp && false) {
                longOperation = new LongOperation();
                longOperation.execute();
            } else {
                activate.setVisibility(View.VISIBLE);
                findViewById(R.id.progressBar).setVisibility(View.GONE);
                mainText.setText("\nSensor Ready!\n");
            }
            //                    }
            break;
        default:
        }
    } else if (i.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
        //HERE TO CANCEL THE VOLUME CONTROL BUTTONS. IDK MAY CAUSE BUGS.
    }
}