waylonion
waylonion

Reputation: 6976

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.

Upvotes: 5

Views: 6479

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006574

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()).

Upvotes: 10

Related Questions