用于停止闹钟的Android按钮无效?

时间:2017-02-20 22:21:13

标签: java android

我目前正致力于将警报纳入更大的项目中。使用datepicker / timepicker,一切正常。一旦达到日期和时间,警报就会启动。我遇到的问题是停止警报的唯一方法是关闭应用程序。我添加了一个按钮(stopAlarm),我认为需要一切来阻止这个待处理的意图,但它仍然没有停止警报?

我可能忽略了一些非常小的东西,但我想知道是否有人可以指出我正确的方向?

谢谢!

MainActivity:

package servicealarmdemo.test2;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity{

DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
Button buttonStopAlarm;
TextView info;

final static int RQS_1 = 1;

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

    info = (TextView)findViewById(R.id.info);
    pickerDate = (DatePicker)findViewById(R.id.pickerdate);
    pickerTime = (TimePicker)findViewById(R.id.pickertime);

    Calendar now = Calendar.getInstance();

    pickerDate.init(
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH),
            null);

    pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
    pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));

    buttonSetAlarm = (Button)findViewById(R.id.setalarm);
    buttonSetAlarm.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Calendar current = Calendar.getInstance();

            Calendar cal = Calendar.getInstance();
            cal.set(pickerDate.getYear(),
                    pickerDate.getMonth(),
                    pickerDate.getDayOfMonth(),
                    pickerTime.getCurrentHour(),
                    pickerTime.getCurrentMinute(),
                    00);

            if(cal.compareTo(current) <= 0){
                //The set Date/Time already passed
                Toast.makeText(getApplicationContext(),
                        "Date/Time Has Already Passed!",
                        Toast.LENGTH_LONG).show();
            }else {
                setAlarm(cal);
            }

            buttonStopAlarm = (Button)findViewById(R.id.stopalarm);
            buttonStopAlarm.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    alarmManager.cancel(pendingIntent);

        }});

}


private void setAlarm(Calendar targetCal){

    info.setText("\n\n***\n"
            + "Deadline has now been set: " + targetCal.getTime() + "\n"
            + "***\n");

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}

}

AlarmReceiver.java:

package servicealarmdemo.test2;

import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;

public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
    ringtone.play();
}
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <DatePicker
            android:id="@+id/pickerdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TimePicker
            android:id="@+id/pickertime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/setalarm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Set Alarm"/>
        <Button
            android:id="@+id/stopalarm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="stopAlarm"
            android:text="Stop Alarm"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

您可以使用不同的方法将方法与视图连接 - setAlarm与代码(onClickListener)连接,stopAlarm与XML连接。这可能是XML连接到view / Activity的方式或stopAlarm方法本身的问题。在stopAlarm方法中进行一些调试以确认它已被调用。

此外,根据this post,您可能需要在获取待处理意图时使用“CURRENT_UPDATE_FLAG”。

public void stopAlarm(View view) {
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);

}

要尝试的另一件事是缓存设置和停止警报之间的意图。而不是重新创建意图。