0

我有这个代码可以在时间选择器设置的警报上触发敬酒消息,除了敬酒消息之外,我还想触发/启动警报音。

这是我在接收器上的代码:

    public void onReceive(Context context, Intent intent) {

        long time = intent.getExtras().getLong("time");
        Toast.makeText(context, "Alarming at "+formatDate(time),
                Toast.LENGTH_LONG).show();
        //Uri alerTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        try {
            Uri alert = getAlarmUri();
            Log.d("alarmCheck","Value of URI: "+alert.toString());
            Ringtone r = RingtoneManager.getRingtone(context, alert);
            r.play()

;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private Uri getAlarmUri() {
        Uri alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alert == null) {
            alert = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (alert == null) {
                alert = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
        }
        return alert;
    }

这是我的主要活动:

   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TimePicker timepicker = (TimePicker) findViewById(R.id.alarm_time);
    Button set_alarm = (Button) findViewById(R.id.set_alarm);

        set_alarm.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                 Calendar c = Calendar.getInstance();
                 c.setTimeInMillis(System.currentTimeMillis());
                 c.set(Calendar.HOUR_OF_DAY, timepicker.getCurrentHour());
                 c.set(Calendar.MINUTE, timepicker.getCurrentMinute());
                 c.set(Calendar.SECOND, 0);
                 Intent in = new Intent(MainActivity.this, OnAlarmReciever.class);
                         in.putExtra("time", c.getTimeInMillis());
                 PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 123456789, in, 0);
                 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                 am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);
            }
        });

    }

在我的接收器上,try catch 块的部分 getRingtone() 返回 null 因此访问 play() 会产生 nullexception .. 任何有关此问题的输入将不胜感激。

谢谢,上帝保佑!

编辑(DDMS Logcat,URI 值)


08-26 08:46:55.764: D/alarmCheck(537): Value of URI: content://settings/system/alarm_alert
08-26 08:46:55.774: D/MediaPlayer(537): Couldn't open file on client side, trying server side
08-26 08:46:55.825: E/MediaPlayer(537): Unable to to create media player
08-26 08:46:55.825: E/RingtoneManager(537): Failed to open ringtone content://settings/system/alarm_alert
08-26 08:46:55.825: W/System.err(537): java.lang.NullPointerException
08-26 08:46:55.844: W/System.err(537):  at com.example.alarmexercise.OnAlarmReciever.onReceive(OnAlarmReciever.java:36)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
08-26 08:46:55.844: W/System.err(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 08:46:55.844: W/System.err(537):  at android.os.Looper.loop(Looper.java:137)
08-26 08:46:55.854: W/System.err(537):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-26 08:46:55.854: W/System.err(537):  at java.lang.reflect.Method.invokeNative(Native Method)
08-26 08:46:55.854: W/System.err(537):  at java.lang.reflect.Method.invoke(Method.java:511)
08-26 08:46:55.854: W/System.err(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-26 08:46:55.864: W/System.err(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-26 08:46:55.874: W/System.err(537):  at dalvik.system.NativeStart.main(Native Method)

** 附加编辑.. 铃声 r = ... r 真正为空的行**

这种默认音调/声音是否可能在模拟器上不起作用?因为我已经阅读了一些关于在模拟器上播放音调的线程,这些线程由于模拟器没有默认声音或无法处理音调/声音而无法工作?.....无法确认这一点,因为很少有人这么说

4

1 回答 1

0

确实,可能是模拟器问题。尝试将 mp3 文件推送到 sdcard 并将其设置为铃声,看看是否有帮助?

于 2012-08-26T06:29:14.837 回答