Making an alarm program - not working

前端 未结 2 753
陌清茗
陌清茗 2020-12-22 06:47

I am attempting to make alarm program. So far I have written an activity in which the user can select the time he wishes the alarm to go off. This is working fine. Now I nee

2条回答
  •  伪装坚强ぢ
    2020-12-22 07:11

    I assume you are missing registering your receiver in Manifest file, With appropriate action string. as given below.

              
                   
                      
                    // can change name/action string as par ur requirement.
            
    

    you need to set same action string in your intent, Remember Action string must be same in Manifest and here intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE"); in java also. then only it will tringger receiver.

    Your code can be changed like given below.

    Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
    intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE");// added line
    
    PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
    
    AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
    alarms.cancel(pIntent);
    
    
    alarms.setRepeating(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis()+1000, 
            AlarmManager.INTERVAL_DAY, 
            pIntent);
    

提交回复
热议问题