【问题标题】:Notification sound not stopping android通知声音不会停止android
【发布时间】:2018-12-01 19:32:37
【问题描述】:

我只是调用一个 Notification 。但它会持续播放声音,除非我拖动通知..

下面是我的代码,现在我正在OREO下面进行测试..

NotificationManager mNotificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "droidudes.zcode";

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String name = "easyTouch";
            String Description = "Best Utility Tool";
            int importance = NotificationManager.IMPORTANCE_HIGH;//IMPORTANCE_HIGH
            NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
            mChannel.setDescription(Description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern();
            mChannel.setShowBadge(false);
            mNotificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext, NOTIFICATION_CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                ///.setVibrate(longArrayOf(0, 1000, 500, 1000))

                .setSmallIcon(R.drawable.icon_of_app)
                .setContentTitle(mContext.getString(R.string.app_name))
                .setContentText("Toucher Hiding Here")
                .setAutoCancel(true);

        PendingIntent pendingIntent = PendingIntent.getService(mContext, 1,
                new Intent(mContext,EasyTouchService.class)
                        .setAction(EasyTouchService.ACTION_SHOW), PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);

        Notification mNotification = mBuilder.build();

        mNotification.flags |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        //mNotification.tickerText = getString(R.string.app_name);
        // setUpAsForeground(message);
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);

通过在上面的代码中注释振动声音,我尝试了多种方法。但仍然可以无限时间播放..

【问题讨论】:

    标签: android android-notifications notificationmanager


    【解决方案1】:

    试试这个:

     final PendingIntent pendingIntent = PendingIntent.getActivity(
                    mCtx,
                    notfication_id,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx);
                    Notification mNotif = builder.setSmallIcon(R.mipmap.logo)
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setContentTitle("title of your notification")
                            .setContentText("details of your notification")
                            .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.logo_full))
                            .build();
    
                    mNotif.flags = Notification.FLAG_AUTO_CANCEL;
                    mNotif.defaults |= Notification.DEFAULT_SOUND;
                    mNotif.defaults |= Notification.DEFAULT_VIBRATE;
    
    
                    NotificationManager notificationManager = (NotificationManager)mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotif);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多