这个问题已经被问了很多,我尝试了很多答案,但是没有用,所以这是最常见的问题
This qustion has been asked alot and i tried alot of the answer and didn't work so this is the most common one
public void onClick(View v) { boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.this ,0, new Intent(MainActivity.this,Notifications.class), PendingIntent.FLAG_NO_CREATE) != null); if (alarmUp) { am.cancel(pend); Intent alarmIntent = new Intent(MainActivity.this,Notifications.class); final PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE); if (pendingIntent != null) { pendingIntent.cancel(); } Toast.makeText(MainActivity.this,"Tweak cleared", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"There is no Tweak!", Toast.LENGTH_LONG).show(); }此按钮在我的MainActivity中,每次按下时我想检查是否存在是警报还是否,那么如果取消了警报 am和吐司按摩,则该应用程序始终在else语句没有调整!中吐司按摩。即使有警报..我是android的初学者,也将不胜感激。.谢谢。
this button is in my MainActivity and everytime it's pressed i want to check if there is an alarm or no then if there is cancel the alarm "am" and toast massage..now the app always toasts the massage in the else statement "There is no Tweak!" even if there is alarm..i'm beginner in android so would appreciate any help..thanks.
编辑
这是MainActivity中另一个启动警报的按钮
this is another button in my MainActivity that starts the alarm
start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { intent = new Intent(getApplicationContext(), Notifications.class); pend = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pend);在我按下另一个取消它的按钮之前,我先按下此按钮。
i press this button before i press the other one that cancels it.
编辑
这是我检查警报并取消警报的另一种尝试。它可以工作,但是当我关闭该应用程序并再次打开它时,它会告诉您没有警报没有任何调整!,即使有警报
this is my other attempt to check for alarm and cancel it..it works but when i close the app and open it again it tells that there is not alarm "there is no tweak!" even if there is one
cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (am != null && pend != null) { am.cancel(pend); pend = null; Toast.makeText(MainActivity.this,"Tweak cleared", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"There is no Tweak!", Toast.LENGTH_LONG).show(); } } });pend和am是在开始按钮中初始化的私有变量
谢谢
推荐答案private AlarmManager mAlarmMgr; private PendingIntent mPendingIntent; if (mAlarmMgr != null && mPendingIntent != null) { mAlarmMgr.cancel(mPendingIntent); mPendingIntent = null; Log.v("alarm", "done"); } else { Log.v("alarm", "no PendingIntent instance to cancel"); }
这就是取消闹钟的方式。
That's how you cancel an alarm.
更多推荐
如何检查AlarmManager是否已经在工作?
发布评论