⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alarmservice.java

📁 Google s android SDK
💻 JAVA
字号:
/*  * Copyright (C) 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.android.samples.app;// Need the following import to get access to the app resources, since this// class is in a sub-package.import com.google.android.samples.R;import android.app.Activity;import android.app.AlarmManager;import android.app.NotificationManager;import android.content.Intent;import android.os.SystemClock;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * <p>Example of scheduling one-shot and repeating alarms.  See * {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and * {@link RepeatingAlarm} for the code run when the repeating alarm goes off. * This demonstrates a very common background that puts together both alarmsand service: a regularily scheduled alarm that results in the execution ofa relatively long-lived service.  An example of where you would use this isfor background retrieval of mail.  In this situation, you don't want toretrieve the mail directly in the alarm's intent receiver, because this wouldblock others while you are working.  Instead, the alarm starts a service thattakes care of retrieving the mail.</p><h4>Demo</h4>App/Service/Alarm Service <h4>Source files</h4><table class="LinkTable">        <tr>            <td class="LinkColumn">src/com/google/android/samples/app/AlarmService.java</td>            <td class="DescrColumn">The activity that lets you schedule the alarm</td>        </tr>        <tr>            <td class="LinkColumn">src/com/google/android/samples/app/AlarmService_Alarm.java</td>            <td class="DescrColumn">This is an intent receiver that executes when the                alarm goes off</td>        </tr>        <tr>            <td class="LinkColumn">src/com/google/android/samples/app/AlarmService_Service.java</td>            <td class="DescrColumn">This is the service that implements our background action,                which is started by the AlarmService_Alarm</td>        </tr>        <tr>            <td class="LinkColumn">/res/any/layout/alarm_service.xml</td>            <td class="DescrColumn">Defines contents of the screen</td>        </tr></table> */public class AlarmService extends Activity{    @Override	protected void onCreate(Bundle icicle)    {        super.onCreate(icicle);        setContentView(R.layout.alarm_service);        // Watch for button clicks.        Button button = (Button)findViewById(R.id.start_alarm);        button.setOnClickListener(mStartAlarmListener);        button = (Button)findViewById(R.id.stop_alarm);        button.setOnClickListener(mStopAlarmListener);    }    private OnClickListener mStartAlarmListener = new OnClickListener()    {        public void onClick(View v)        {            // This is the intent receiver who will be run when the alarm            // goes off.  We just create an intent with an explicit class name            // to have our own receiver (which has been published in            // AndroidManifest.xml) instantiated and called.            Intent intent = new Intent(AlarmService.this, AlarmService_Alarm.class);            // We want the alarm to go off 30 seconds from now.            long firstTime = SystemClock.elapsedRealtime();            // Schedule the alarm!            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,                            firstTime, 30*1000, intent);            // Tell the user about what we did.            NotificationManager nm = (NotificationManager)                getSystemService(NOTIFICATION_SERVICE);            nm.notifyWithText(R.string.repeating_scheduled,                      getText(R.string.alarm_service_scheduled),                      NotificationManager.LENGTH_LONG,                      null);        }    };    private OnClickListener mStopAlarmListener = new OnClickListener()    {        public void onClick(View v)        {            // Create the same intent that was scheduled.            Intent intent = new Intent(AlarmService.this, AlarmService_Alarm.class);            // And cancel the alarm.            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);            am.cancel(intent);            // Tell the user about what we did.            NotificationManager nm = (NotificationManager)                getSystemService(NOTIFICATION_SERVICE);            nm.cancel(R.string.repeating_scheduled);            nm.notifyWithText(R.string.repeating_unscheduled,                      getText(R.string.alarm_service_unscheduled),                      NotificationManager.LENGTH_SHORT,                      null);        }    };}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -