我已经实现了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)
答案 0 :(得分:0)
您的图标设置为R.mipmap.ic_launcher
。顾名思义,这表明您正在尝试将应用启动器图标用作通知图标。不过,这不是有效的通知图标。
使用Android Studio中的图像资产向导创建专用的通知图标-您将在向导中为“图标类型”找到专用的“通知图标”类别。然后,切换为使用该资源,而不是使用R.mipmap.ic_launcher
,然后看看您是否有更好的结果。
答案 1 :(得分:-1)
听,您要在其中创建通知通道的类必须继承应用程序类enter image description here