0101-APP-Alarm -controller
2014-05-11 21:46
211 查看
展示通过AlarmManager显示单次和重复定时调用
这个应用我在红米和HTC S510e上都进行了测试,在s510上达到到预期的效果但是红米上却有很大的延迟甚至reciver甚至无法收到消息
注释:
RepeatingAlarm.class
OneShotAlarm.class
都是一个简单的Recevier
这个应用我在红米和HTC S510e上都进行了测试,在s510上达到到预期的效果但是红米上却有很大的延迟甚至reciver甚至无法收到消息
/* * Copyright (C) 2007 The Android Open Source Project * * 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.example.android.apis.app; // Need the following import to get access to the app resources, since this // class is in a sub-package. import com.example.android.apis.R; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; 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; import android.widget.Toast; import java.util.Calendar; /** * 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. * <h4>Demo</h4> App/Service/Alarm Controller <h4>Source files</h4> <table class="LinkTable"> <tr> <td class="LinkColumn">src/com.example.android.apis/app/AlarmController.java</td> <td class="DescrColumn">The activity that lets you schedule alarms</td> </tr> <tr> <td class="LinkColumn">src/com.example.android.apis/app/OneShotAlarm.java</td> <td class="DescrColumn">This is an intent receiver that executes when the one-shot alarm goes off</td> </tr> <tr> <td class="LinkColumn">src/com.example.android.apis/app/RepeatingAlarm.java</td> <td class="DescrColumn">This is an intent receiver that executes when the repeating alarm goes off</td> </tr> <tr> <td class="LinkColumn">/res/any/layout/alarm_controller.xml</td> <td class="DescrColumn">Defines contents of the screen</td> </tr> </table> */ public class AlarmController extends Activity { Toast mToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm_controller); // Watch for button clicks. Button button = (Button)findViewById(R.id.one_shot); button.setOnClickListener(mOneShotListener); button = (Button)findViewById(R.id.start_repeating); button.setOnClickListener(mStartRepeatingListener); button = (Button)findViewById(R.id.stop_repeating); button.setOnClickListener(mStopRepeatingListener); } private OnClickListener mOneShotListener = new OnClickListener() { public void onClick(View v) { // When the alarm goes off, we want to broadcast an Intent to our // BroadcastReceiver. Here we make an Intent with an explicit class // name to have our own receiver (which has been published in // AndroidManifest.xml) instantiated and called, and then create an // IntentSender to have the intent executed as a broadcast. Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 30); // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled, Toast.LENGTH_LONG); mToast.show(); } }; private OnClickListener mStartRepeatingListener = new OnClickListener() { public void onClick(View v) { // When the alarm goes off, we want to broadcast an Intent to our // BroadcastReceiver. Here we make an Intent with an explicit class // name to have our own receiver (which has been published in // AndroidManifest.xml) instantiated and called, and then create an // IntentSender to have the intent executed as a broadcast. // Note that unlike above, this IntentSender is configured to // allow itself to be sent multiple times. Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 15*1000; // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled, Toast.LENGTH_LONG); mToast.show(); } }; private OnClickListener mStopRepeatingListener = new OnClickListener() { public void onClick(View v) { // Create the same intent, and thus a matching IntentSender, for // the one that was scheduled. Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // And cancel the alarm. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled, Toast.LENGTH_LONG); mToast.show(); } }; }
注释:
RepeatingAlarm.class
OneShotAlarm.class
都是一个简单的Recevier
相关文章推荐
- 安卓ApiDemos学习 app/Alarm/AlarmController
- 【起航计划 018】2015 起航计划 Android APIDemo的魔鬼步伐 17 App->Alarm->Alarm Service
- iOS -App主流框架UINavigationController && UITabBarController的简单使用
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 1 部分
- Ext.app.controller的refs
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
- jni/../../Classes/AppDelegate.cpp:102: error: undefined reference to 'TestController::TestC...
- 服务器安装实验二十七 SCVMM之APPController自助服务
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x19862
- Cannot declare class app\home\controller\Cases because the name is already in use
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
- IOS app中有两个UITabBarController,即在第一个页面有4个按钮,进入子页面底部也有几个按钮
- UITabBarController 笔记(一)AppDelegate中加UITabBarController 为 rootViewController
- System Center 2012 R2 自助门户2—App Controller
- Swift中利用AppDelegate实现调用指定ViewController中的函数
- 部署System Center App Controller 2012 Service Pack 1 (1)
- 如何更优雅的实现ExtJS的前端MVC模式下一个App多个Controller的动态加载
- ExtJS中app(controller,application)相关问题
- ng-app指令定义了应用, ng-controller 定义了控制器
- UITabBarController 笔记(一)AppDelegate中加UITabBarController 为 rootViewController