How to check if AlarmManager is already working?

自闭症网瘾萝莉.ら 提交于 2020-01-06 15:53:27

问题


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();
            }

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.

edit

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.

edit

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 and am are prviate variables which are initialized in the "start" button

Thanks


回答1:


Because you are using the flag "PendingIntent.FLAG_NO_CREATE", it indicates that if the described PendingIntent does not already exist, then simply return null instead of creating it.

From your code, you never created the PendingIntent before, so it will always return null.




回答2:


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.



来源:https://stackoverflow.com/questions/40456093/how-to-check-if-alarmmanager-is-already-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!