设置多个闹钟的不同铃声

时间:2018-09-13 08:28:20

标签: android alarmmanager

我在做什么

我正在制作一个警报应用程序,该应用程序在MainActivity上的RecyclerView上具有多个警报界面。每个警报项目均包含一个selectAlarmTone按钮,该按钮会打开一个铃声选择器对话框。在AlarmAdapter代码内部的selectAlarmTone按钮的onClick方法中,我已将回调接口调用到MainActivity,该接口旨在打开铃声选择器。 MainActivity还具有onActivityResult方法,该方法将selectAlarmTone按钮的文本更改为所选铃声的标题。

问题

我面临的问题是,当我作为用户为一个警报项目选择一个警报音时,所有警报项目的警报音都会发生变化,这是意外的。只是,所有其他按钮的按钮文本不会更改。但是所有警报项的铃声的确会更改,打开所有其他警报项的铃声选择器都可以看到它。

AlarmAdapter.java

....
public AlarmViewHolder(View itemView) {
        super(itemView);
        ...
        selectAlarmTone = (Button) itemView.findViewById(R.id.select_alarm);
        setAlarmButton = (ToggleButton) itemView.findViewById(R.id.button_set_alarm);
        ...

        /*
         * selectAlarmTone button opens up a Ringtone Picker dialog
         *
         * */
        final Ringtone ringtone = RingtoneManager.getRingtone(mContext, defaultRingtoneUri);
        selectAlarmTone.setText(ringtone.getTitle(mContext));

        selectAlarmTone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                mAdapterCallback.onMethodCallback(selectAlarmTone, defaultRingtoneUri, ringtone);
            }
        });
        ...
}

MainActivity.java

...
AlarmAdapter.AdapterCallback callback = new AlarmAdapter.AdapterCallback() {
    @Override
    public void onMethodCallback(Button selectAlarmTone, Uri defaultRingtoneUri, Ringtone ringtone) {

        mSelectAlarmTone = selectAlarmTone;
        /*
         * selectAlarmTone button opens up a Ringtone Picker dialog
         *
         * */

        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        if (currentRingtoneUri == null)
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, defaultRingtoneUri);
        else intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentRingtoneUri);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Alarm Tone");
        startActivityForResult(intent, REQUEST_RINGTONE_PICKER);
    }

    ...
};
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1:
                AlarmManagerBroadcastReceiver.alarmUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), AlarmManagerBroadcastReceiver.alarmUri);
                mSelectAlarmTone.setText(ringtone.getTitle(getApplicationContext()));
                currentRingtoneUri = AlarmManagerBroadcastReceiver.alarmUri;
                break;

            default:
                break;
        }
    }
}
....

0 个答案:

没有答案