gpt4 book ai didi

java - Android 停止闹钟按钮不起作用?

转载 作者:行者123 更新时间:2023-12-01 08:51:40 25 4
gpt4 key购买 nike

我目前正在开发一个警报,将其包含在一个更大的项目中。日期选择器/时间选择器一切正常。一旦到达日期和时间,警报就会启动。我遇到的问题是停止警报的唯一方法是关闭应用程序。我添加了一个按钮(stopAlarm),其中包含我认为停止此待处理 Intent 所需的一切,但它仍然没有停止警报?

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

谢谢!

主要 Activity :

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>

最佳答案

您有不同的机制将方法与 View 连接起来 - setAlarm 使用代码 (onClickListener),而 stopAlarm 使用 XML。这可能是 XML 连接到 View /Activity 的方式有问题,也可能是 stopAlarm 方法本身有问题。在 stopAlarm 方法中进行一些调试以确认它被调用。

此外,根据 this post ,在获取待处理 Intent 时,您可能需要使用“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);

}

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

关于java - Android 停止闹钟按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42355264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com