0

I'm currently developing a simple Flashlight app. I want to have a notification which can be pressed by the user and works like a toggle for the flashlight, so the user can turn the flashlight on and off, even if the app runs in the background and isn't visible to the user. I already experiemented a lot with notifications, but the only thing I managed to achieve was a notification that started an activity which was then brought to the foreground. As I understand it, notifications can only launch activities. So how can I achieve that the activity stays in the background and just turns the flashlight on/off?

  • You can also use widget that works for On/Off.i think it will be more acceptable by the Application users. – Jawad Zeb Feb 08 '15 at 21:12

1 Answers1

0

As I understand it, notifications can only launch activities.

You are wrong. You can start a service with PendingIntent.getService.

Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);

Source: Answer to "Start Service from Notification"

And if I understand you correctly, you probably also want to use Builder.setOngoing to keep your notification visible after click.

Community
  • 1
  • 1
atastrumf
  • 183
  • 1
  • 7