Reputation: 2025
In the Android documentation for running a Service in the forground, the following example code is provided:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
However, this code does not work. Firstly, the constructor used for Notification is deprecated. Second, the method setLatestEventInfo(Context, String, String, PendingIntent) is no longer included in the Notification class. When I eliminate these and create a notification the correct way, an error occurs which looks like this:
Caused by: java.lang.NullPointerException: class name is null
at android.content.ComponentName.<init>(ComponentName.java:114)
at android.app.Service.startForeground(Service.java:654)
at com.vcapra1.motionsensors.MainActivity.startService(MainActivity.java:53)
EDIT: Here is the code I am using, from the link provided by @ RusheelJain:
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(ctx, MotionMonitorService.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, (int) System.currentTimeMillis(), intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification notification = new Notification.Builder(ctx)
.setContentTitle("Monitoring Motion")
.setContentText("No events yet...")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
// notificationManager.notify(1, notification);
startForeground(1, notification);
When I use the notificationManager.notify(1, notification);
line, a notification is created, but it is not persistent and does not start the Service (which, of course, is not meant to happen). However, when I use the startForeground(1, notification);
line, the app crashes with the above stack trace.
So my final question is: what is the correct way to start a Service that will keep running even if the app is closed? I have checked several sources and they all include the method which I found on the Android docs.
Upvotes: 2
Views: 3930
Reputation: 1006584
Call startForeground()
with a valid Notification
from a service lifecycle method (e.g., onCreate()
, onStartCommand()
) or a method invoked by one of those lifecycle methods (e.g., onHandleIntent()
of an IntentService
).
For example, this service uses a foreground Notification
while a download is going on in onHandleIntent()
:
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.foredown;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloader extends IntentService {
public static final String ACTION_COMPLETE=
"com.commonsware.android.downloader.action.COMPLETE";
private static int NOTIFY_ID=1337;
private static int FOREGROUND_ID=1338;
public Downloader() {
super("Downloader");
}
@Override
public void onHandleIntent(Intent i) {
try {
String filename=i.getData().getLastPathSegment();
startForeground(FOREGROUND_ID,
buildForegroundNotification(filename));
File root=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
root.mkdirs();
File output=new File(root, filename);
if (output.exists()) {
output.delete();
}
URL url=new URL(i.getData().toString());
HttpURLConnection c=(HttpURLConnection)url.openConnection();
FileOutputStream fos=new FileOutputStream(output.getPath());
BufferedOutputStream out=new BufferedOutputStream(fos);
try {
InputStream in=c.getInputStream();
byte[] buffer=new byte[8192];
int len=0;
while ((len=in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
out.flush();
}
finally {
fos.getFD().sync();
out.close();
c.disconnect();
}
stopForeground(true);
raiseNotification(i, output, null);
}
catch (IOException e2) {
stopForeground(true);
raiseNotification(i, null, e2);
}
}
private void raiseNotification(Intent inbound, File output,
Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis());
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), inbound.getType());
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
private Notification buildForegroundNotification(String filename) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setOngoing(true);
b.setContentTitle(getString(R.string.downloading))
.setContentText(filename)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setTicker(getString(R.string.downloading));
return(b.build());
}
}
(from this sample project)
Your stack trace indicates that you are trying to call startForeground()
from MainActivity
, which should not be possible and certainly is not an appropriate pattern.
Upvotes: 3
Reputation: 384
You have to use Notifiation.Builder instead of the Notification's class constructor.
Here is an example of starting foreground service with actions:
private Notification getForegroundNotification() {
Intent showTaskIntent = MyActivity.createStartIntent(this, mContext);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
showTaskIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getString(R.string.title))
//.setContentText(getString(R.string.notification_text))
.setSmallIcon(R.drawable.notification_white)
.setContentIntent(contentIntent);
Intent shutdownActionIntent = new Intent(this, MyService.class);
shutdownActionIntent.setAction(SHUTDOWN_NOTIFICATION_ACTION);
PendingIntent shutdownPendingIntent = PendingIntent.getService(
getApplicationContext(),
1,
shutdownActionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(android.R.drawable.ic_lock_power_off, getString(R.string.shutdown_action), shutdownPendingIntent);
if(isRunningJellybeanOrLater()) {
builder.setPriority(Notification.PRIORITY_MAX);
return builder.build();
} else {
return builder.getNotification();
}
}
This is how you start it:
void handleStartAction() {
Notification notification = getForegroundNotification();
startForeground(FOREGROUND_NOTIFICATION_ID, notification);
}
Upvotes: 2