Android : Do something when a headset is plugged in when my app is running in the background. (if possible i want to do it with broadcast receivers)

on Thursday, March 19, 2015


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!


0 comments:

Post a Comment