锁屏状态下点亮屏幕,并弹出闹钟提示信息

Micky米 2013-04-14 06:33:29
加精
锁屏状态下点亮屏幕,并弹出闹钟提示信息,可以在锁屏界面上取消闹钟;使用广播接收闹钟定时:


下面是例子里的核心代码如下


android 设置定时闹钟(包括提醒一次和循环提醒):
Intent intent = new Intent(MainActivity.this,MyAlarmBroadCast.class);
/**
* context 指定实例
* requestcode 可以作为闹钟的唯一性标识
* intent The Intent to be broadcast
* flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,
*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

/***
* 获取全局定时器的服务管理器
*/
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

/**
* 指定的任务只会执行一次,如果该pendingIntent指定的任务已经被执行过了,那么该方法直接会被cancel掉。
* set(int type, long triggerAtTime, PendingIntent operation)
* type 指定定时模式。
* triggerAtTime 触发任务的时间。该参数和定时模式息息相关
* operation 该参数指定一个广播Intent,当时间到了时,系统会广播里面的intent,触发相应的广播接收者执行某些操作,比如响铃……
*/
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
/**
* 通过该方法指定的任务会一直间隔执行,第三个参数就指定了执行的时间间隔
* 如果我们想取消的话,请使用:alarmManager.cancel(pendingIntent);
* 注意,这里的pendingIntent要和setRepeating方法中的一致哦。
*/
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);

android取消指定闹钟:
Intent intent = new Intent(AlarmReminderActivity.this,MyAlarmBroadCast.class);
/**
* context 指定实例
* requestcode 可以作为闹钟的唯一性标识,根据这个的不同来删除闹钟
* intent The Intent to be broadcast
* flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,
*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

/***
* 获取全局定时器的服务管理器
*/
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(pendingIntent);

android唤醒锁定屏幕:
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
mWakelock.acquire();

该方法的使用一定要伴随mWakelock.release();否则会报异常;
推荐使用方法:在唤醒屏幕显示的activity的onResume方法中唤醒,在onPause方法中release;


如何让activity显示在锁屏界面上:
首先要唤醒锁定屏幕,然后设置activity的属性,使它可以显示在锁屏界面上;
唤醒屏幕的方法参考上面;
activity的设置包括两部分:
1-onCreate方法中设置如下代码:
super.onCreate(savedInstanceState);
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.act_alarmreminder);
2-AndroidManifest.xml对该activity的声明中设置属性:
android:label="@string/app_name"
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:taskAffinity=""
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"(这一行很重要)
android:configChanges="orientation|keyboardHidden|keyboard|navigation"
...全文
4373 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
juelianxuelang 2014-07-17
  • 打赏
  • 举报
回复
谢谢分享,学习看看
原来我是成成 2014-04-12
  • 打赏
  • 举报
回复
我的是到设置的闹钟时间不能唤醒屏幕,需要怎么做啊
_VV_ 2013-04-24
  • 打赏
  • 举报
回复
八错八错
henryaaaaaaaaaaaa 2013-04-20
  • 打赏
  • 举报
回复
不错啊,学习下
u010224781 2013-04-19
  • 打赏
  • 举报
回复
u010318762 2013-04-19
  • 打赏
  • 举报
回复
学习,很好很强大
花小美0616 2013-04-19
  • 打赏
  • 举报
回复
谢谢 试试看!
Graphy 2013-04-18
  • 打赏
  • 举报
回复
顶楼主,有用~~
heronghui123456 2013-04-18
  • 打赏
  • 举报
回复
学习了,感谢分享
绝望依然 2013-04-18
  • 打赏
  • 举报
回复
兄台,继续加油!!!
绝望依然 2013-04-18
  • 打赏
  • 举报
回复
绝望依然 2013-04-18
  • 打赏
  • 举报
回复
Oo伍oO 2013-04-17
  • 打赏
  • 举报
回复
好东西!! 学习了。。。。。
Micky米 2013-04-16
  • 打赏
  • 举报
回复
http://apk.hiapk.com/html/2013/04/1378107.html?module=256&info=CVRzfNBjkpE%3D 我写了个应用发布到android市场了,大家去试用一下,希望多提意见啊~欢迎交流
u010327307 2013-04-16
  • 打赏
  • 举报
回复
不错哦 好好好好
lhw7791086 2013-04-16
  • 打赏
  • 举报
回复
顾小林 2013-04-16
  • 打赏
  • 举报
回复
留个记号,顶一个!!!!!!!!
fbiboss 2013-04-16
  • 打赏
  • 举报
回复
手机的程序很有意思啊,比电脑上的好玩
Wison 2013-04-15
  • 打赏
  • 举报
回复
刚开始接触安卓,先存一下,以后肯定能用得上!
d19901217 2013-04-15
  • 打赏
  • 举报
回复
引用 2 楼 d19901217 的回复:
点亮了屏幕,键盘锁未解、测试是通过按power键上锁的。android4.2。
是我的问题 ,在activity里有效,dialog上无效
加载更多回复(10)

80,466

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧