【发布时间】:2019-04-06 11:30:38
【问题描述】:
我已经实现了一个startForeground() 方法来启动在onCreate() 中调用的持续通知。在两个不同的类中有两种用法。我正在测试 API 27 (Android 8.1)
第一次使用(PictureCapturingService.java):
onCreate():
@Override
public void onCreate() {
super.onCreate();
Context mContext = this.getApplicationContext();
context = this.getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground();
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Capturing Image").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground():
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle("Capturing Image")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
第二次使用(VideoRecordService.java):
从onCreate() 和startRecord() 调用
onCreate():
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Sending Message");
}
else {
startForeground(1234, new Builder(this).setContentTitle("Sending Message").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startRecord():
private void startRecord() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Video Recording");
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground:
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(String str) {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(str)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
我收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raudram.barathikannamma, PID: 18871
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
【问题讨论】:
-
基于
channel=null,似乎存在第三条路径,您正在尝试使用没有通道的Notification启动前台服务。 -
您在哪个 Android 版本中遇到上述错误?恐怕问题出在
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.mipmap.ic_launcher).build());而不是startMyOwnForeground方法上。 -
@madlymad 我目前正在 API 27 上进行测试
-
@CommonsWare 先生,我整个项目中只有
startForeground方法中的five usages,这里全部提到了5个。错误可能出在针对 API -
我正在使用以下逻辑来创建频道和通知github.com/madlymad/uptime/blob/master/app/src/main/java/com/… 并且从未遇到过问题...我注意到 2 之间的唯一区别是频道命名系统可能不喜欢“点” "
标签: java android service android-notifications background-service