如何使用AlarmManager从我的应用程序设置警报音?

时间:2011-07-29 13:25:54

标签: android

如何使用AlarmManager从我的应用程序设置警报音?

我的RingTonePicker方法: -

private void startRing()
{
   Intent i = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
   startActivityForResult(i, ACTIVITY_SET_RINGTONE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data )
{
    switch (resultCode) {
        case RESULT_OK:
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            Log.i("Sample", "uri " + uri);
            RingtoneManager.setActualDefaultRingtoneUri(this,RingtoneManager.TYPE_NOTIFICATION, uri);
                 break;
           }
    super.onActivityResult(requestCode, resultCode, data);
}

我的BroadCast接收方法: -

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "matched", Toast.LENGTH_LONG).show();
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context
            .getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);
}

问题是只有振动器振动没有铃声正在播放。

请帮助我。

先谢谢。

1 个答案:

答案 0 :(得分:4)

你可以用这个 -

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mp= MediaPlayer.create(getBaseContext(), alert);
mp.setVolume(100, 100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp){
mp.release();
}
});

vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE);
vibrator.vibrate(400);