0

I want to make it so when one plugs in there headset a notification icon appears. I've made it so when the phone turns on this runs which starts the MainActivity class which has the code for the notification icon in the OnCreate method so it just automatically starts. The problem with that is that it starts the whole activity and app, which I don't want. I just want the icon to appear. How could I go about this? Thank You!

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
}

The above code starts the MainActivity on boot.

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
Fernando
  • 450
  • 1
  • 7
  • 22
  • Can you please elaborate your question.Where do you need notification?Please add more code to this. – Siddharth_Vyas Oct 14 '13 at 07:04
  • @androiduser I really need a Notification Icon to appear whenever the user connects there headphones in. I want the notification to appear in the very top of the phone (Notification Area, pull that down for the Notification Drawer) I already have the code for that written and it works. (Updated the main question with it) But I just want that code to be ran whenever the user plugs in there headphones. I don't want the app to start or anything, just for that icon to appear. – Fernando Oct 14 '13 at 16:23

2 Answers2

0

As I wrote in your last question, add a <receiver> in your manifest that listens for the ACTION_HEADSET_PLUG broadcast. The documentation shows Intent extras that you can use to find out if the headset is plugged in (state), etc.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I got that. Using this receiver. ` ` But how would I make an if else statement saying what to do with that. and where. that's what confuses me – Fernando Oct 12 '13 at 22:05
  • @FernandoRamirez: I have no idea what you are talking about. You "want to make it so when one plugs in there headset a notification icon appears". This requires a dozen or so lines of code in an `onReceive()` method of a `BroadcastReceiver` set to listen for `ACTION_HEADSET_PLUG` broadcasts. – CommonsWare Oct 12 '13 at 22:09
  • I just need to know how to make it work in the background, like for it to detect it without having to open up the app. Like in this new question. I've solved all my problems except this one, it's my last. http://stackoverflow.com/questions/19368767/how-to-call-this-even-when-the-app-hasnt-even-started – Fernando Oct 14 '13 at 22:37
0

I posted a solution that allows you to monitor the headphone state (wired and bluetooth), over here. This solution could easily be extended to allow you to display a notification.

Community
  • 1
  • 1
Phil Haigh
  • 4,522
  • 1
  • 25
  • 29