-1

I want to do something when a headset is plugged in when my app is running in the background. (if possible I want to do it with a broadcast receiver)

I tried the code below:

--ReceiveBroadcast--

package com.example.openmusiconheadsetconnect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ReceiveBroadcast extends BroadcastReceiver {
    public ReceiveBroadcast() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Received!",Toast.LENGTH_LONG).show();
    }
}

--Manifest--

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.openmusiconheadsetconnect" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name=".ReceiveBroadcast"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.HEADSET_PLUG" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Thank you!

Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • check this link : http://stackoverflow.com/questions/13610258/how-to-detect-when-a-user-plugs-headset-on-android-device-opposite-of-action-a – AADProgramming Mar 19 '15 at 15:12

2 Answers2

1

Your code is correct, but as far as I know, you cannot put the HEADSET_PLUG filter on the manifest. Instead, create a receiver in its own class, and make it listen for USER_PRESENT (screen unlocked) or BOOT_COMPLETED in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
<receiver android:name="classes.myReceiver" >        
             <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </receiver>

When triggered by such events, your receiver should start the service:

public class myReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context ctx, Intent intent) {     
        Intent service = new Intent(ctx, VoiceLaunchService.class);
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            ctx.stopService(service);  
            }
        }

The service will now register the receiver that will be listening to the HEADSET_PLUG intent, in its onCreate method:

@Override
public void onCreate() {
    super.onCreate();   
    speechReconRx=new SpeechReconControlReceiver(this);//"this" will allow you to call service's methods from the receiver
    registerReceiver(speechReconRx, new IntentFilter(Intent.HEADSET_PLUG)); 
    }

It's is a hassle, but you'll need it if you don't want to use an activity. It is google's fault for not letting us put PLUG receivers in the manifest! Finally make the Broadcast that will take action when the headset is plugged in.

public class SpeechReconControlReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context ctx, Intent intent) {     
       Log.e("joshcsr","HEADSET PLUGGED!");
        if(intent.getStringExtra("command")!=null){
            c=intent.getStringExtra("command");
            }
        //run some methods from the service
        if (c.equals("resume")) {           
            sService.resume();  
            }

        if (c.equals("pause")) {            
            sService.pause();  
            }

        if (c.equals("stop")) {         
            sService.stop();  
            }       
        }
    }

To wrap, up you will need: *A receiver for the BOOT/Screen unlock events. *A Service to hold everything that will run on the background and to register your headset listening broadcast. *And a receiver for the headset Plug, that will take action and call methods hosted in the service.

I've did this yesterday, and it works from Jelly bean to Lollipop ...and perhaps even older versions. Cheers.

Josh
  • 6,251
  • 2
  • 46
  • 73
  • Can I just create a `mainactivity` without a `user interface`? I want my app to run in the background. – Thomas Vos Mar 19 '15 at 15:23
  • @SuperThomasLab Sure, with a slightly different approach. Check out my improved answer above... – Josh Mar 19 '15 at 15:51
0

First you'll need permission to start app in background after boot is completed.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and also specify this in your broadcast receiver,

<receiver android:name=".YourBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

Then create a service that run your application in background, and inside the service use AudioManager.isWiredHeadsetOn() to check if the headset is plugged in. And if so, do the task you want.

while(AudioManager.isWiredHeadsetOn()){
    //your task goes here
}

Also add the permission in manifest

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Apurva
  • 7,871
  • 7
  • 40
  • 59