I want to make it so when one plugs in there headset, when doing whatever on there phone, not just begin on the app, a Notification icon appears in the Notification Area (Very top) I already have the Notification Icon code
//Notification Icon Starts
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.icon_notification, "Icon Notification", System.currentTimeMillis());
Context context=MainActivity.this;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(this, "Notification Icon", "Touch for more options", contentIntent);
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
nm.notify(0, notification);
//Notification Icon Ends
Now I just need it so when you plug in your headset that shows. So I made a new class with this, which detects if it's plugged in or not and then logs it. And that all works
public class MusicIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d("unplugged", "Headset was unplugged");
break;
case 1:
Log.d("plugged", "Headset is plugged");
break;
default:
Log.d("uh", "I have no idea what the headset state is");
}
}
}
So what I did was tried putting that Notification Icon code inside the case 1 so that when it detected that it was plugged in, it would run that. But it doesn't, instead I get a ton of errors. Here's a screenshot http://prntscr.com/1xblhb I just can't think of any other way to approach this, I've been stuck on this for so long. So if you could help me and try to say it in beginner terms it would mean the world to me. Thank you so much.