Alarm Manager Example

前端 未结 10 1438
情话喂你
情话喂你 2020-11-21 05:12

I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.

Can anyone help me with a basic alar

10条回答
  •  梦谈多话
    2020-11-21 05:40

    I tried the solution from XXX and while it did initially work, at some point it stopped working. The onReceive never got called again. I spent hours trying to figure out what it could be. What I came to realize is that the Intent for whatever mysterious reason was no longer being called. To get around this, I discovered that you really do need to specify an action for the receiver in the manifest. Example:

    
        
            
            
        
     
    

    Note that the name is ".Alarm" with the period. In XXX's setAlarm method, create the Intent as follows:

    Intent i = new Intent("mypackage.START_ALARM");
    

    The START_ALARM message can be whatever you want it to be. I just gave it that name for demonstration purposes.

    I have not seen receivers defined in the manifest without an intent filter that specifies the action. Creating them the way XXX has specified it seems kind of bogus. By specifying the action name, Android will be forced to create an instance of the BroadcastReceiver using the class that corresponds to the action. If you rely upon context, be aware that Android has several different objects that are ALL called context and may not result in getting your BroadcastReceiver created. Forcing Android to create an instance of your class using only the action message is far better than relying upon some iffy context that may never work.

提交回复
热议问题