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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With