我正在使用一个使用前台服务的应用程序.
为此,我正在从服务的onStartCommand回调函中调用startForeground(id,Notification).
为此,我正在从服务的onStartCommand回调函中调用startForeground(id,Notification).
我使用通知构建器来创建我的通知,但是当我将其传递给startForeground时,只有在我设置的情况下才会显示代码文本,其他所有内容都会转为默认,即标题说“正在运行,而我设置为”在线“
还有我在notification.Builder中使用setText和setInfo方法设置的任何东西,而不是像“触摸更多信息或停止应用程序”显示的默认文本.
以下是相关代码:
服务:
private final int NOTIFICATION_ID=1; @Override public int onStartCommand(Intent intent,int flags,int startId) { Toast.makeText(this,"EDI: Core service Started",Toast.LENGTH_LONG).show(); startForeground(NOTIFICATION_ID,CoreServiceNotification.getNotification(this,"EDI is online","Online","Online and running","EDI just started")); return super.onStartCommand(intent,flags,startId); }
CoreServiceNotification:
public class CoreServiceNotification { public static Notification getNotification(Context context,String title,String text,String info,String tickerText){ Notification.Builder notificationBuilder= new Notification.Builder(context); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(text); notificationBuilder.setContentInfo(info); notificationBuilder.setTicker(tickerText); notificationBuilder.setLights(0x00ffff00,1000,0); return notificationBuilder.build(); } }
结果:
解决方法
我认为在创建通知时首先需要设置smallIcon.
Notification.Builder notificationBuilder= new Notification.Builder(context); notificationBuilder.setSmallIcon(R.drawable.yourIcon); // then set other staff...
这是Cousera Android类的另一个简单例子,
Click here