首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android 9下的startForeground()

Android 9下的startForeground()
EN

Stack Overflow用户
提问于 2020-03-02 20:50:16
回答 2查看 192关注 0票数 0

不久前,我为Android 7.1开发了一个应用程序。尝试在另一部智能手机(安卓9)上运行应用程序失败,因为使用了startForeground()。

代码语言:javascript
复制
Intent notificationIntent = new Intent(getApplicationContext(), MyService.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);

            Notification notification = new Notification.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("title")
                    .setContentText("Foreground-Service running")
                    .setContentIntent(pendingIntent).build();

            startForeground(101, notification);

我得到了以下错误:

代码语言:javascript
复制
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test2app, PID: 14754
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:1737)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我添加了这个权限:

代码语言:javascript
复制
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

我该如何解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2020-03-02 21:28:23

代码语言:javascript
复制
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    
    String str = "ch_id_i";
            if (VERSION.SDK_INT >= 26) {
                NotificationChannel notificationChannel = new NotificationChannel(str, "name", 
                NotificationManager.IMPORTANCE_LOW);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(SupportMenu.CATEGORY_MASK);
                notificationChannel.enableVibration(false);
                notificationChannel.setShowBadge(false);
               notificationmanager.createNotificationChannel(notificationChannel);
            }

builder = new NotificationCompat.Builder(context, str);
票数 0
EN

Stack Overflow用户

发布于 2020-03-02 23:26:20

所以它起作用了:

代码语言:javascript
复制
Intent notificationIntent = new Intent(getApplicationContext(), MyService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);
            createNotificationChannel();
            NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.ic_launcher_background);
                builder.setContentTitle("title");
                builder.setContentText("text");
                builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
                builder.setContentIntent(pendingIntent);
            startForeground(1, builder.build());

    private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Foregroundservice MyService.java";
        String description = "Benachrichtigung über das Aktivieren des Foreground-Service";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

很好的参考:https://developer.android.com/training/notify-user/build-notification#java

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60489242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档