I'm learning Android service now, and i want to implement a foreground service. But the notification doesn't come out. And this is my code:
public class ForegroundService extends Service {
private static final String TAG = "ForegroundService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.w(TAG, "in onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
Intent nfIntent = new Intent(this, DActivity.class);
builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0))
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.airplane))
.setContentTitle("Title")
.setSmallIcon(R.drawable.airplane)
.setContentText("content to show")
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
startForeground(110, notification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "in onDestroy");
stopForeground(true);
}
}
And i found this warning info in Logcat:
Attempted to start a foreground service (ComponentInfo{com.demo.service.servicedemo/com.demo.service.servicedemo.ForegroundService}) with a broken notification (no icon: Notification(pri=0 contentView=null vibrate=null sound=default defaults=0x1 flags=0x40 color=0x00000000 vis=PRIVATE)or incomplete notification info) lackOfIcon:false autoComplemtion:true not to show in notification center
My android version is: 7.1.1
Thanks for your time.
User contributions licensed under CC BY-SA 3.0