代码之家  ›  专栏  ›  技术社区  ›  Zar E Ahmer

通知声音未停止android

  •  0
  • Zar E Ahmer  · 技术社区  · 6 年前

    我只是打电话通知。但它一直在播放声音,除非我拖动通知。。

    下面是我的代码,现在我正在测试下面的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);
    

    我已经尝试了多种方法,通过评论振动声音在上述代码。但仍然可以无限期地播放声音。。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gourango Sutradhar    6 年前

     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);