/*
* 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.tyd;
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.
*/
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) {
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
// Intent intent1 = new Intent(AlarmController.this, OneShotAlarm.class);
// intent1.putExtra("gjq", "intent01");
// PendingIntent sender1 = PendingIntent.getActivity(AlarmController.this,
// 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
//
//// Intent intent2 = new Intent(AlarmController.this, OtherAlarm.class);
// Intent intent2 = new Intent(AlarmController.this, OneShotAlarm.class);
// intent2.putExtra("gjq", "intent02");
// PendingIntent sender2 = PendingIntent.getActivity(AlarmController.this,
// 0, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
//
// am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+2*1000, sender1);
// am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 4*1000, sender2);
// 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.getActivity(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, 5);
// Schedule the alarm!
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 += 5*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 5*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();
}
};
}
没有合适的资源?快使用搜索试试~ 我知道了~
Alarm_Sample.rar_android
共48个文件
class:22个
java:8个
xml:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 100 浏览量
2022-09-24
04:52:30
上传
评论
收藏 75KB RAR 举报
温馨提示
android中基于alarmmanager管理的appdemo程序
资源推荐
资源详情
资源评论
收起资源包目录
Alarm_Sample.rar (48个子文件)
Alarm_Sample
bin
classes.dex 11KB
com
tyd
OneShotAlarm.class 918B
AlarmService.class 2KB
R$layout.class 497B
AlarmController$3.class 2KB
AlarmService_Service$1.class 949B
R$id.class 609B
R$drawable.class 402B
AlarmService$2.class 1KB
R$attr.class 307B
Alarm_SampleActivity.class 2KB
AlarmController$2.class 2KB
AlarmService_Service.class 3KB
R.class 454B
RepeatingAlarm.class 655B
AlarmService_Service$2.class 790B
R$string.class 1KB
AlarmController.class 1KB
Alarm_SampleActivity$1.class 957B
AlarmService$1.class 1KB
OtherAlarm.class 489B
AlarmController$1.class 2KB
Alarm_SampleActivity$2.class 954B
Alarm_Sample.apk 23KB
resources.ap_ 16KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
stat_sample.png 364B
icon.png 4KB
values
strings.xml 2KB
drawable-mdpi
icon.png 3KB
layout
onshot.xml 493B
other.xml 464B
alarm_controller.xml 2KB
main.xml 549B
alarm_service.xml 2KB
assets
default.properties 363B
gen
com
tyd
R.java 3KB
proguard.cfg 1KB
src
com
tyd
AlarmController.java 6KB
RepeatingAlarm.java 1KB
AlarmService_Service.java 4KB
OtherAlarm.java 308B
OneShotAlarm.java 1KB
Alarm_SampleActivity.java 1KB
AlarmService.java 3KB
.project 848B
.classpath 280B
AndroidManifest.xml 2KB
共 48 条
- 1
资源评论
寒泊
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功