我在我的应用中使用推送通知。我可以播放推送的默认声音。现在我想使用一些Mp3。我不在哪里将mp3放在项目中以及如何在活动中使用它。请帮帮我。
答案 0 :(得分:3)
将文件放在原始文件夹中。
如果您想使用.ogg文件,请使用:
Thread t = new Thread()
{
public void run()
{
MediaPlayer player = null;
player = MediaPlayer.create(context,R.raw.push_sound);
player.start();
try
{
Thread.sleep(player.getDuration()+100);
}
catch (InterruptedException e)
{
}
}
}
};
t.start();
修改强>:
当您在BroadcastReceiver中收到通知时,请使用以下代码,然后在该活动类中调用活动。
使用以下代码播放声音文件。
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
答案 1 :(得分:0)
你必须得到IntentReceiver的帮助:
public class IntentReceiver extends BroadcastReceiver {
private static final String logTag = "PushSample";
public static String APID_UPDATED_ACTION_SUFFIX = ".apid.updated";
public static String gcmId="";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(logTag, "Received intent: " + intent.toString());
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
// Id
String ap_id = intent.getStringExtra(PushManager.EXTRA_APID);
System.out.println("IntentReceiver::- ID::-" + ap_id);
Log.i(logTag,
"Received push notification. Alert: "
+ intent.getStringExtra(PushManager.EXTRA_ALERT)
+ " [NotificationID=" + id + "]");
logPushExtras(intent);
}
}
收到推送时会触发PushManager.ACTION_PUSH_RECEIVED。您需要在清单中声明IntentReceiver。
这是一个很好的教程:http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html