【问题标题】:Starting Sound/Alarm when APP Receive GCM MessageAPP收到GCM消息时启动声音/警报
【发布时间】:2013-06-19 09:31:15
【问题描述】:

我正在开发一个用于教育目的的 Android 应用程序。

这个APP有一个GCMService并且运行良好,但是当APP收到来自GCM Service的某个消息时,我试图启动一个简单的声音或警报。

我该怎么做? 我正在搜索信息,但总是看到人们启动服务或活动,我认为这很容易。

谢谢大家!

【问题讨论】:

  • @ypnotize 你想在收到消息时播放声音吗?
  • 我现在实现了 gcm 消息,但现在我尝试在 30 秒左右播放声音。

标签: android alarmmanager google-cloud-messaging


【解决方案1】:

您可以将代码写入GCMIntentServiceonMessage() 喜欢

@Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message = getString(R.string.gcm_message);
        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
        //Write here for sound notification 
        //for example 
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone ringer = RingtoneManager.getRingtone(getApplicationContext(), notification);
        ringer.play();
    }

当APP收到某条消息时启动简单的声音或警报 来自 GCM 服务

您可以将条件放入上述方法并播放声音。喜欢

if ("XYZ".equals(message)) {
//Then Play a sound
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone ringer = RingtoneManager.getRingtone(getApplicationContext(), notification);
        ringer.play();

}

【讨论】:

  • 效果很好,但我需要在 30 秒左右播放声音 ALARM,而不是默认铃声(在我的电话中现在发出环聊铃声)。如何修改声音?如何播放大约 30 秒的声音?
猜你喜欢
  • 2013-03-07
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多