Воспроизведение только звука уведомлений по умолчанию (Android)

Как я могу воспроизвести только звук уведомления (без запуска уведомления в строке состояния)? Я хочу звук уведомления по умолчанию и воспроизводить его точно так же, как звук уведомления. Можно ли добиться этого с помощью MediaPlayer?


person Rafael    schedule 26.04.2012    source источник
comment
Вы можете сделать это с помощью медиаплеера, если просто найдете звуковой файл. Вы должны быть в состоянии найти звук по умолчанию где-то в источниках Android. Ознакомьтесь с классом уведомлений и этим вопросом.   -  person keyser    schedule 26.04.2012


Ответы (1)


вы можете попробовать это

private void sendNotification(String title, String messageBody,String tag) {

    Intent intent;
    intent = new Intent(this, MainActivity.class);
    intent.setAction(tag);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    String channel_id=this.getResources().getString(R.string.app_name);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setChannelId(channel_id)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel mChannel = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(channel_id, channel_id, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
} 
person arya    schedule 01.05.2020