我正在开发一个使用前台服务的应用程序。
为此,我从服务的 onStartCommand 回调内部调用 startForeground(id,Notification)
。
我使用通知构建器来创建我的通知,但是当我将它传递给 startForeground 时,仅显示我设置的代码文本,其他所有内容都变为默认值,即标题显示“正在运行,而我将其设置为”在线”
还有我在通知中使用 setText 和 setInfo 方法设置的任何内容。Builder 不会显示,而是显示默认文本,如“触摸以获取更多信息或停止应用程序”。
相关代码如下:
服务:
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);
}
核心服务通知:
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
关于java - Android:启动前台不会正确显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148734/