Android handles events of the Alarm clock Alarm, androidalarm

Source: Internet
Author: User

Android handles events of the Alarm clock Alarm, androidalarm

The previous blog posts have been constantly sharing Phone-related knowledge, which is also a knowledge note. However, it is inevitable that you will encounter problems in other modules during your work, therefore, when solving these problems, we can easily record and share these knowledge. Some knowledge is hard to understand, and it is easy to understand. At the same time, some knowledge is time-sensitive, such as the change of Android versions and the change of Phone architecture. Therefore, it is enough to hope that your notes can help some children's shoes.

Reprint please be sure to indicate the source: http://blog.csdn.net/yihongyuelan

In Android, what should I do if the application needs to do something about it when the alarm is triggered? First, we need to receive the event, and then consider whether to stop or snooze it ). The following descriptions are available in the Code packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmService. java:

35    // A public action send by AlarmService when the alarm has started.36    public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";3738    // A public action sent by AlarmService when the alarm has stopped for any reason.39    public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";

You can see from the comments that the system provides two actions to broadcast alarm events, including: com. android. clock. ALARM_ALERT and com. android. clock. ALARM_DONE, that is, the com. android. clock. ALARM_ALERT, which triggers com. android. clock. ALARM_DONE. That is to say, You need to register a broadcast listener to listen to broadcast events:

IntentFilter filter = new IntentFilter();filter.addAction("com.android.deskclock.ALARM_ALERT");filter.addAction("com.android.deskclock.ALARM_DONE");registerReceiver(mReceiver, filter);
Here the mReceiver is the BroadCastReceiver object, which processes the event in onReceive. When the alarm is triggered, AlarmActivity is displayed. the java path is packages/apps/javasclock/src/com/android/javasclock/alarms/AlarmActivity. java, which provides two important alarm operation interfaces: stop and snooze:
49    // AlarmActivity listens for this broadcast intent, so that other applications50    // can snooze the alarm (after ALARM_ALERT_ACTION and before ALARM_DONE_ACTION).51    public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";5253    // AlarmActivity listens for this broadcast intent, so that other applications54    // can dismiss the alarm (after ALARM_ALERT_ACTION and before ALARM_DONE_ACTION).55    public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
This method provides two broadcast receiving interfaces: com. android. javasclock. ALARM_SNOOZE and com. android. javasclock. ALARM_DISMISS. The former is used for snooze and the latter is used for stop. You can see the processing of this action in AlarmActivity as follows:
184        IntentFilter filter = new IntentFilter(AlarmService.ALARM_DONE_ACTION);185        filter.addAction(ALARM_SNOOZE_ACTION);186        filter.addAction(ALARM_DISMISS_ACTION);187        registerReceiver(mReceiver, filter);118    private BroadcastReceiver mReceiver = new BroadcastReceiver() {119        @Override120        public void onReceive(Context context, Intent intent) {121            String action = intent.getAction();122            Log.v("AlarmActivity - Broadcast Receiver - " + action);123            if (action.equals(ALARM_SNOOZE_ACTION)) {124                snooze();125            } else if (action.equals(ALARM_DISMISS_ACTION)) {126                dismiss();127            } else if (action.equals(AlarmService.ALARM_DONE_ACTION)) {128                finish();129            } else {130                Log.i("Unknown broadcast in AlarmActivity: " + action);131            }132        }133    };
That is to say, you can use the broadcast method in the code to control the stop and snooze of the alarm:
private void stopAlarm() {    Intent intent = new Intent();    intent.setAction("com.android.deskclock.ALARM_DISMISS");    sendBroadcast(intent);}    private void snoozeAlarm() {    Intent intent = new Intent();    intent.setAction("com.android.deskclock.ALARM_SNOOZE");    sendBroadcast(intent);}
The Demo is attached. when the alarm is triggered, it is received and stopped/Snooze. The Code is as follows:
    private void stopAlarm() {        Intent intent = new Intent();        intent.setAction("com.android.deskclock.ALARM_DISMISS");        sendBroadcast(intent);    }        private void snoozeAlarm() {        Intent intent = new Intent();        intent.setAction("com.android.deskclock.ALARM_SNOOZE");        sendBroadcast(intent);    }    Handler mHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case 0:                    Log.i("Seven", "stop alarm");                    stopAlarm();                    break;                case 1:                    Log.i("Seven", "snooze alarm");                    snoozeAlarm();                    break;                default:                    break;            }        }    };        private void registerAlarmReceiver() {        mReceiver = new MyReceiver();        IntentFilter filter = new IntentFilter();        filter.addAction("com.android.deskclock.ALARM_ALERT");        filter.addAction("com.android.deskclock.ALARM_DONE");        registerReceiver(mReceiver, filter);    }    class MyReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            if (intent.getAction().equals("com.android.deskclock.ALARM_ALERT")) {                Log.i("Seven","ALARM_ALERT");                mHandler.sendEmptyMessageDelayed(0,15); //stop alarm                //mHandler.sendEmptyMessageDelayed(1,15); //snooze alarm            }else if (intent.getAction().equals("com.android.deskclock.ALARM_DONE")) {                Log.i("Seven","ALARM_DONE");            }        }    }
Some may be wondering why the sendEmptyMessageDelayed method of handler is used instead of directly calling stopAlarm and snoozeAlarm. If you use the stopAlarm and snoozeAlarm methods directly, it will have no effect. A delay of at least 15 ms will be required.

Add some English translations so that foreign children's shoes can also get some help.

How to stop an alarm in android

How to turn off the alarm clock on the droid

How to cancel the android alarm

Android Alarm not turning off programmatically

Automatically silence the sound volume of Android Phone programmatically


In android, how does the alarm clock program listen to the system time?

Windows applications implement functions by calling APIs.
The function you want to do is simple.
CTime tm;
Tm = CTime: GetCurrentTime ();
Then, determine if the tm time is your set alarm time.
Yes. Call MMplay () to play a song.

The same is true for smart machines.
No matter what platform has an API, you can check the manual.

Android alarm principle, details

Please refer to the following [android alarm clock implementation principles], which may be helpful to you.
Let's take a look at the steps from creating an alarm to an alarm:

1. Create an alarm:

01
// Obtain the AlarmManager instance
02
Final AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
03

04
// Instantiate Intent
05
Intent intent = new Intent ();
06
// Set Intent action attributes
07
Intent. setAction ("com. test. BC_ACTION ");
08
Intent. putExtra ("msg", "it's time to go to the meeting! ");
09
// Instantiate PendingIntent
10
Final PendingIntent pi = PendingIntent. getBroadcast (MainActivity. this, 0,
11
Intent, 0 );
12
// Obtain the system time
13
Final long time = System. currentTimeMillis ();
14
Am. set (AlarmManager. RTC_WAKEUP, time + 5000, sender); // an alarm is triggered in 5 seconds.
15

16
// Set the button to click the event
17
SetBtn. setOnClickListener (new OnClickListener (){
18
@ Override
19
Public void onClick (View v ){
20
// Repeat the prompt, starting from the current time, with an interval of 5 seconds
21
Am. setRepeating (AlarmManager. RTC_WAKEUP, time,
22
5*1000, pi );
23
}
24
});

Register a broadcast receiver in AndroidMainfest. xml

1
<Cycler android: name = "mycycler">
2
<Intent-filter>
3
<Action android: name = "com. test. BC_ACTION"/>
4
</Intent-filter>
5
</Cycler>

2. Define an AlarmReceiver extends BroadcastReceiver to receive broadcasts and bring up the alarm notification view.

The above uses an AlarmManage. Let's take a look at its alarm handling process, functions, and examples.
Alarm handling process: corresponding to AlarmManage, there is an AlarmMana... the remaining full text>
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.