I have been stuck on this for weeks now...I can not get this to work. What I am trying to do is for my app to recognize when a user plugs in there earphones/headphones into the phone, and when they do, for it to show a notification icon. Here is my class. I have added the
public class ItudeMobileBroadcastReceiver extends BroadcastReceiver {
int NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;
@Override
public void onReceive(Context context, Intent intent) {
/* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/
Log.d("Autobooted", "it booted");
Toast laToast = Toast.makeText(context, "I have booted", 5);
laToast.show();
//It boots so it shows that toast. Rest doesn't work though
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d("unplugged", "Headset was unplugged");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
Toast unpluggedToast = Toast.makeText(context, "Headset has been disconnected", Toast.LENGTH_SHORT);
unpluggedToast.show();
break;
case 1:
Log.d("plugged", "Headset is plugged");
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.notification_icon, "short", false);
Toast pluggedToast = Toast.makeText(context, "Headset has been connected", Toast.LENGTH_SHORT);
pluggedToast.show();
break;
default:
Log.d("uh", "I have no idea what the headset state is");
Toast defaultToast = Toast.makeText(context, "No headset detected", Toast.LENGTH_SHORT);
defaultToast.show();
}
}
}
private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
Intent contentIntent = new Intent();
//Text that it says in the notification area when the notification appears
String dropDownText = "Notification Title";
Notification n = new Notification(R.drawable.notification_icon, "Notification Title", System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(context, MainActivity.class); //Activity that will be launched if notification is clicked
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
n.setLatestEventInfo(context, "Notification Title", "Bottom Line", pending);
mNotificationManager.notify(NOTIFICATION_ID, n);
}
}
And here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.testreciever.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:enabled="true"
android:name=".ItudeMobileBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MODIFY_AUDIO_SETTINGS" />
</intent-filter>
</receiver>
<activity
android:name="com.example.testreciever.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testreciever.Hope"
android:label="@string/title_activity_hope" >
</activity>
</application>
</manifest>
On boot it does show the "it booted" toast, so I know it's working. But it's not showing the notification icon when I plug in the earphones. I really just don't know what to do anymore. If you can help, please do. Thank You so much.
I know the notification code works, because if i put it in an activity class and i launch the app it works, but I don't want to have to launch the app. I just want it to work on boot.