Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Service from Notification

is it possible to start a service from a notification. The normal way of starting an activity is working perfectly, but I need some pre checks of data before actually starting the app.

I've tested it with including a valid service in the notification intent, but nothing happens.

like image 679
AlexVogel Avatar asked Jun 21 '11 08:06

AlexVogel


People also ask

How do I start a service from notification?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to src/MainActivity.

How do I trigger a service in Android?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

Can we start service without activity?

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to. The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher.


1 Answers

It is possible to start a service from a notification.

You have to use PendingIntent.getService instead of pendingIntent.getActivity.

Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class); PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);  Notification notification = new Notification(icon, tickerText,System.currentTimeMillis()); notification.setLatestEventInfo(mContext,contentTitle , contentText, pendingIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;  NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);  notificationManager.notify(CALLER_ID_NOTIFICATION_ID, notification); 
like image 121
Kjetil Eide Avatar answered Sep 30 '22 22:09

Kjetil Eide