1. 程式人生 > >Android 播放提示音

Android 播放提示音

1.Ringtone

import android.net.Uri;
import android.media.RingtoneManager;
import android.media.Ringtone;

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play(); 

2.Notification
import android.app.Notification;
import android.app.NotificationManager;
import android.net.Uri;
import android.media.RingtoneManager;
import android.media.Ringtone

NotificationManager nm = (NotificationManager)getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Notification.Builder noti = new Notification.Builder(MainActivity.this).setWhen(0);
noti.setSound(ringUri);
final Notification notification = new Notification.BigTextStyle(noti).build();
nm.notify(123, notification);