2015-07-14 2 views
3

Мне нужно управлять уведомлением, и для этого мне нужно управлять настраиваемым звуком, когда он приходит. Итак, вы знаете, как мы можем это сделать?Как добавить пользовательский звук уведомления в Android

Я уже скопировал звуковой файл в свою сырую папку, поэтому у кого-нибудь есть идеи, как я могу реализовать его в своем проекте.

Заранее спасибо.

+0

Http: // stackov erflow.com/questions/13760168/how-to-set-notification-with-custom-sound-in-android –

ответ

9

Во-первых, сделать папку в Resource (Рез) назовите его сырой и поместить файл (YOUR_SOUND_FILE.MP3) в нем и не использовать ниже строк кода для пользовательских звук

NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, 
      SlidingMenuActivity.class); 
    notificationIntent.putExtra("isInbox", true); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

Использование эти строки кода для пользовательских звук

notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play 


    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 
2
   Intent i = new Intent(this, MainActivity.class); 
       PendingIntent pi = PendingIntent.getActivity(this, 0, i, 
         PendingIntent.FLAG_CANCEL_CURRENT); 

       NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
         .setContentTitle("I want food") 
         .setContentText(notificationcontent) 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setContentIntent(pi) 
         .setAutoCancel(true) 
         .setDefaults(Notification.FLAG_ONLY_ALERT_ONCE); 
       NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound); 
       mp.start(); 
       manager.notify(73195, builder.build()); 
Смежные вопросы