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?
Asked
Active
Viewed 106 times
0
-
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 Answers
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.
-
So I would have to implement a service to start/stop my flashlight? – IAskDumbQuestions Feb 08 '15 at 21:45
-
Using my approach, yes. This can help you: http://stackoverflow.com/questions/20594251/android-camera-getparameters-crashes-app – atastrumf Feb 08 '15 at 22:01