如何检查AlarmManager是否已经在工作?

编程入门 行业动态 更新时间:2024-10-24 10:26:13
本文介绍了如何检查AlarmManager是否已经在工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题已经被问了很多,我尝试了很多答案,但是没有用,所以这是最常见的问题

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是否已经在工作?

本文发布于:2023-11-27 10:25:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!