如何通过在切换按钮之间切换来停止播放声音?

时间:2015-03-17 20:39:32

标签: android android-intent android-activity alarmmanager android-pendingintent

我制作了一个闹钟,在那里我使用了拨动开关来启动闹钟。我想要的是,一旦用户再次点击按钮,它应该停止警报(响铃)。在ON和OFF之间切换分别开始和停止。

但是现在,我只能开始发出警报。事实上它永远不会停止。如果我关闭按钮,声音播放得更快,这很奇怪。我怎么能阻止它? 我已经粘贴了我假设问题发生的代码的唯一部分。如果您需要查看我的整个应用程序代码,我会发布它。 [注意:其他一切正常]

MainActivity:

    package com.mycompany.alarmclock;
//I haven't post the imported stuff here, they are in my file
import android.support.v7.app.ActionBarActivity;
import java.util.Calendar;
public class MainActivity extends Activity {

    AlarmManager alarmManager;
    private PendingIntent pendingIntent;//an action to be performed by other/foreign application
    private TimePicker alarmTimePicker;//In where the user set the time
    private static MainActivity inst;
    private TextView alarmTextView;//the area where alarm message/notification will be displayed


    public static MainActivity instance() {
        return inst;
    }

    public void onStart() {
        super.onStart();
        inst = this;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
        alarmTextView = (TextView) findViewById(R.id.alarmText);
        ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    }


    public void onToggleClicked(View view) {
        if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
            Log.d("MainActivity", "Alarm On");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
            Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
//In this else statement I need to add the logic so that the alarm clock stops after clicking the toggle button
        } else {//else if the toggle button is pressed again, switch off the alarm /sound
            alarmManager.cancel(pendingIntent);
            setAlarmText("");
            Log.d("MyActivity", "Alarm Off");
        }

    }

    public void setAlarmText(String alarmText) {
        alarmTextView.setText(alarmText);

    }

}

AlarmReceiver:

package com.mycompany.alarmclock;
//I haven't posted my imported stuff here, they are on my file.
import android.support.v4.content.WakefulBroadcastReceiver;

import java.net.URI;
public class AlarmReceiver extends WakefulBroadcastReceiver {
 @Override
    public void onReceive(final Context context, Intent intent) {
        MainActivity inst=MainActivity.instance();
        inst.setAlarmText("Get Up! Get up!");
        Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();

        //this will send a notification message
        ComponentName comp = new ComponentName(context.getPackageName(),
                AlarmService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

1 个答案:

答案 0 :(得分:2)

在关闭代码的切换按钮中,您可以添加以下行:

Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.stop();

编辑:

查看代码后,这可能不适用于所有情况。如果您的活动被终止并且警报被触发,则不会打开任何活动来停止铃声。即使通过通知启动,切换按钮也将处于错误状态。触摸通知时,可能应停止铃声。

当警报触发时,它会调用AlarmReceiver,它会调用AlarmService。 AlarmService是创建通知的内容。它指定在按下通知时将调用的Intent。这是您的主要活动,即AlarmActivity。

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, AlarmActivity.class), 0);

您可以在意图中传递额外内容以通知您的活动警报处于振铃状态并随后将其解除,或者只需将您的切换设置为开启并让用户将其解雇。

我没有看到调用completeWakefulIntent(intent)会释放你的唤醒锁定,但这可能是样本的一个问题。

修改

铃声需要原始实例才能阻止它。正确的方法是创建一个服务,但这个演示有点涉及。我建议创建一个静态引用和一个静态方法来访问它。

AlarmReceiver:

    private static Ringtone mRingtone  = null;
...
    mRingtone  = RingtoneManager.getRingtone(context, alarmUri);
    mRingtone.play();
...
}

    public static void stopRingtone() {
        mRingtone.stop();
    }

AlarmActivity:

        AlarmReceiver.stopRingtone();
        alarmManager.cancel(pendingIntent);
        setAlarmText("");
        Log.d("MyActivity", "Alarm Off");