Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a Foreground Service in Activity

How can I start a service in Activity?

I tried to call:

Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.app_name),getText(R.string.app_name), pendingIntent);
    startForeground(1, notification);

However, it "Cannot resolve method 'startforeground(int, android.app.Notification)'"

What do I do to call startForeground? I tried getApplicationContext() to no avail.

like image 583
waylonion Avatar asked Dec 01 '14 00:12

waylonion


1 Answers

How can I start a service in Activity?

Call startService().

However, it "Cannot resolve method 'startforeground(int, android.app.Notification)'"

startForeground() is a method on Service. Your Service will need to call startForegrond() upon itself (e.g., in the service's onCreate()).

like image 181
CommonsWare Avatar answered Oct 12 '22 19:10

CommonsWare