首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:闹钟播放自己的声音

Android:闹钟播放自己的声音
EN

Stack Overflow用户
提问于 2012-08-13 15:33:48
回答 4查看 10.2K关注 0票数 1

我的应用程序有报警功能。我的要求是在闹钟响起时播放自己的声音,但我无法做到这一点。它只在闹钟响起时显示通知。我想要播放的声音文件在Res.下面的raw文件夹中,我正在发布我的代码:

在我的activity类中:

代码语言:javascript
复制
Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        AlarmIntent.putExtra("Ringtone", 
                Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
        PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 
                (60 * 1000), (24 * 60 * 60 * 1000), Sender);

在receiver类中:

代码语言:javascript
复制
public void onReceive(Context context, Intent intent) { 

    Intent in = new Intent(context, SnoozeEvent.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
    manager.notify(1, notification);  
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-13 15:42:10

您可以设置自定义声音在您的捆绑或进入SD Card.only设置路径的声音文件作为一个通知sound.use下面的代码。

代码语言:javascript
复制
notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
票数 4
EN

Stack Overflow用户

发布于 2012-08-13 15:50:36

尝试更改这两行:

代码语言:javascript
复制
//In your activity:
AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid));
....   
//In your reciever
notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));
票数 1
EN

Stack Overflow用户

发布于 2012-08-13 15:37:07

尝试使用此代码来发出您自己的通知:

代码语言:javascript
复制
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note  = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis());
note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
int nid = 123456789;
manager.notify(nid, note);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11929929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档