0

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.

Fernando
  • 450
  • 1
  • 7
  • 22

2 Answers2

0

you dont have context set up right in most places. Context is not MainActivity.this you dont even have scope to your mainactivity. There is a context variable in your onReceive just use that.

to get a system service from a non activity/service you need to use

context.getSystemService()

you are also trying to create a variable called intent when one was already created so you need another name.

in summation look at what the errors are saying and make those changes

tyczj
  • 71,600
  • 54
  • 194
  • 296
0

Found the answer to this finally

I declared this outside of all the methods

int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

Then in the onReceive method I called the following:

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);

Then declared the following method:

private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
        // This is who should be launched if the user selects our notification.
        Intent contentIntent = new Intent();

        // choose the ticker text
        String tickerText = "ticket text";

        Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());

        PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);

        n.setLatestEventInfo(context, "1", "2", appIntent);

        mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
    }
Fernando
  • 450
  • 1
  • 7
  • 22