首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >警报后活动触发器(RTC_WAKEUP),但屏幕仍然是黑色的

警报后活动触发器(RTC_WAKEUP),但屏幕仍然是黑色的
EN

Stack Overflow用户
提问于 2018-06-17 09:25:32
回答 1查看 707关注 0票数 2

我正在设置一个框架应用程序,讨论如何在Android中对警报做出反应。当手机醒着的时候,一切正常工作,当手机睡着的时候,警报会触发(我甚至可以激活振子),但是屏幕保持不动(完全是黑色的)。我不知道为什么。欢迎任何帮助!

这是MainActivity中设置alarmManager的代码片段(E1是一个EditText,用于要求用户选择秒来触发警报):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public void startAlarm(View view) {
    EditText E1 = (EditText) findViewById(R.id.et1);
    int i = Integer.parseInt(E1.getText().toString());
    Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
    PendingIntent pending_intent = 
    PendingIntent.getBroadcast(this.getApplicationContext(),
        98989898, intent, 0); // 98989898 : some big number
    AlarmManager alarmManager = (AlarmManager) 
        getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 
        System.currentTimeMillis() + i*1000, pending_intent);
    Toast.makeText(this, "Alarm set in: " + i + " seconds", 
    Toast.LENGTH_LONG).show();
}

警报是由alarmBroadcastReceiver接收到的。它本身什么也不做,它立即转移到UserResponseActivity:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // We're creating a new intent that's going to start the UserResponseActivity
        Intent in = new Intent(context, UserResponseActivity.class);
        // This boolean just makes it easier to check if the Activity has been started from
        // this class
        in.putExtra("lock", true);
        // You need to add this to your intent if you want to start an Activity fromm a class
        // that is not an Activity itself
        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Now we just start the Activity
        context.startActivity(in);
    }
}

然后,UserResponseActivity是一个正常的活动,目前有来自Android的样板屏幕。稍后,我计划添加一些代码,请求用户的响应。在造物时,它展示一个祝酒词,并在短时间内激活振动器:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class UserResponseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_response);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        /* Show a success toast*/
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_LONG).show();
        /* Vibrate shortly */
        Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

}

如果您需要它,下面是我清单中启用警报接收器和振动器服务的代码行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<uses-permission android:name="android.permission.VIBRATE" />

<receiver android:name=".AlarmBroadcastReceiver" />

正如前面提到的,如果当手机处于清醒状态时,所有的警报都能正常工作: UserResponseActivity显示在屏幕上,振动器启动。然而,如果当手机睡着时警报触发,振动器就会响(因此手机被唤醒,UserResponseActivity被呼叫),但是屏幕仍然是黑色的。当我随后打开电话时,UserResponseActivity就在上面。

为了让UserResponseActivity打开屏幕,我需要做什么?

谢谢!

添加(在VicJordan的答复后):

@VicJordan:我将您的代码粘贴到onCreate of UserResponseActivity.java中,如下所示,但是得到了两个错误,我无法找出我在这2行代码中做错了什么:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        activity.setTurnScreenOn(true);

( a) Android不能解析符号'O_MR1‘b)它可以在第2行解析'activity’。我以为为“这”而改变活动会起作用,但不行。我真的想不出其他方法来引用我所从事的活动

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class UserResponseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_response);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        // Turn on the screen
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            activity.setTurnScreenOn(true);
        } else {
            final Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }

        /* Show a success toast*/
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_LONG).show();
        /* Vibrate shortly */
        Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

你有什么建议吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-21 17:06:21

要在您的活动中打开屏幕,您可以使用在……上面,但是在API 27中已经不推荐使用FLAG_TURN_SCREEN_ON标志,所以您可以从API 27开始使用Activity.setTurnScreenOn(真)

以下是代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        this.setTurnScreenOn(true);
    } else {
        final Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }

有关更多信息,请查看此官方信息:https://developer.android.com/training/scheduling/wakelock

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50898909

复制
相关文章
如何为Spark应用启用Kerberos的Debug日志
在CDH集群启用了Kerberos后,在执行Spark作业时难免会遇到由于Kerberos认证问题导致作业运行失败的时候,那我们需要针对Spark作业进行调试,通过一些Debug日志查看认证失败的原因。本篇文章Fayson主要介绍如何为Spark的Driver和Executor的JVM启用Kerberos的Debug日志。
Fayson
2018/11/16
2.4K0
如何为Hive2启用Kerberos认证
温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。 Fayson的github: https://github.com/fayson/cdhproject 提示:代码块部分可以左右滑动查看噢 1.文档编写目的 ---- 在前面的文章Fayson介绍了《如何在CDH集群中安装Hive2.3.3》,本篇文章Fayson主要介绍如何为已安装好的Hive2服务启用Kerberos认证。 内容概述: 1.部署环境说明 2.添加Kerberos配置 3.验证Kerberos认证 4.总结 测试环境: 1.C
Fayson
2018/07/12
2.8K0
如何为 .NET CLI 启用 Tab 自动补全
本文介绍了如何为四种 shell(PowerShell、Bash、zsh 和 fish)配置 Tab 自动补全。 对于其他 shell,请参阅相关文档,了解如何配置 tab 自动补全。
用户4268038
2022/01/05
9820
记录日志
日志级别:debug<info<warn<error application.yml配置日志 logging: file: target/app.log level: ROOT: WARN cn.devmar: TRACE import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class SampleClass{ private static
华创信息技术
2019/11/08
7810
日志记录
访问应用服务器的请求都需要拥有一定权限,如果说每访问一个服务都需要验证一次权限,这个对效率是很大的影响。可以把权限认证放到 API 网关来进行。目前比较常见的做法是,用户通过登录服务获取 Token,把它存放到客户端,在每次请求的时候把这个 Token 放入请求头,一起发送给服务器。API 网关要做的事情就是解析这个 Token,知道访问者是谁(鉴定),他能做什么/访问什么(权限)。说白了就是看访问者能够访问哪些 URL,这里根据权限/角色定义一个访问列表。如果要实现多个系统的 OSS(Single Sign On 单点登录),API 网关需要和 CAS(Central Authentication Service 中心鉴权服务)做连接,来确定请求者的身份和权限。
用户1880875
2021/09/07
1.2K0
如何在不重启Yarn服务的情况下启用DEBUG日志记录
为了解决Yarn问题,需要为不同的服务启用调试。但是,在生产集群中,可能无法立即重启Yarn服务。本篇文章Fayson主要介绍如何在不重启Yarn服务的情况下为ResourceManager、JobHistory等服务启用DEBUG级别日志记录。
Fayson
2018/10/23
1.7K0
如何在不重启Yarn服务的情况下启用DEBUG日志记录
MongoDB日志记录
为了在发生故障时提供持久性,MongoDB使用预写日志记录到磁盘journal文件中。
MongoDB中文社区
2020/11/11
2.8K0
MongoDB日志记录
mysql日志记录
log-bin = /path/mysql-bin #其记录日志文件名为mysql-bin.index,mysql-bin.000001(注:重启或者单个文件超出限制会+1)
93年的老男孩
2019/12/18
4.7K0
python 日志记录
#!/bin/env python #--*-- coding=utf8 --*-- # # Author: ablozhou # E-mail: ablozhou@gmail.com # # Copyright 2010 ablozhou # # Distributed under the terms of the GPL (GNU Public License) # # hzdq is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # 2010.3.14 写文件,log级别常数定义 import datetime import sys import traceback import codecs import types #log编码全部按utf8处理 loglevels = {'stdout':['info','debug','warn','error','fatal'], 'file':['info','debug','warn','error','fatal'] } logfile = 'logs.txt' class log4py: def __init__(self,modulename='gloabal', loglevel=loglevels, filename='log4py.txt'): self.filename = filename #self.flag = set(loglevel['stdout']+loglevel['file']) self.loglevel = loglevel self.modulename = modulename self.fcname = None class function(): def __init__(self,fcname,parent): parent.debug('enter ',fcname) self.fcname = fcname self.parent = parent def __del__(self): self.parent.debug('exit ',self.fcname) def dbgfc(self,fcname): '''set debug function name''' f = None if 'debug' in self.flag: f = self.function(fcname,self) return f def _gettime(self): return datetime.datetime.now().isoformat() def outstd(self,*fmt): s = self.fmtstr(*fmt) print s def outfile
py3study
2020/01/03
9070
[CodeIgniter4]-记录日志信息
你可以通过 log_message() 方法将信息记录在本地日志文件中,并且必须在第一个参数中指定错误的”级别”,来表明这个信息的类型(debug,error等)。 第二个参数就是信息本身:
landv
2020/03/05
1.3K0
如何为Ubuntu Dock图标启用最小化点击功能?
我们可以将我们最喜欢和最常用的应用程序固定在Dock上,以便在Ubuntu 18.04 LTS桌面中快速启动它们。默认情况下,Ubuntu Dock位于桌面的左侧。但是,您可以将其移动到屏幕的顶部、底部和右侧。
会长君
2023/04/25
1.7K0
【Java AWT 图形界面编程】AWT 简介 ( AWT 核心类继承体系 )
Java 中 使用 AWT 和 Swing 进行 图形界面开发 , AWT 是 抽象窗口工具集 , Abstract Window Toolkit , AWT 功能比较简单 , Swing 提供了更加丰富的界面组件库 ;
韩曙亮
2023/03/30
6780
【错误记录】Java AWT 图形界面编程报错 ( Exception in thread “main“ java.awt.AWTError: BoxLayout can‘t be shared )
尝试使用 Panel 实现线性布局 , 为 Panel 设置 BoxLayout 布局管理器 ;
韩曙亮
2023/03/30
6580
【错误记录】Java AWT 图形界面编程报错 ( Exception in thread “main“ java.awt.AWTError: BoxLayout can‘t be shared )
【说站】mysql Binlog日志如何启用
启用binlog,通过配置 /etc/my.cnf 或 /etc/mysql/mysql.conf.d/mysqld.cnf 配置文件的 log-bin 选项:
很酷的站长
2022/11/23
1.6K0
Laravel 记录SQL日志
Laravel 默认只在sql语法错误时提示完整的sql日志,但实际情况接口慢,筛选条件和预期不符等,都需要看到sql语句,通过sql语句判断问题所在
崔哥
2023/01/01
1.6K0
SpringBoot AOP 记录操作日志、异常日志
我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能。在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因都要到服务器去查询日志才能找到,这样也不能对发生的异常进行统计。我们可以在需要的方法中增加记录日志的代码,和在每个方法中增加记录异常的代码,最终把记录的日志存到数据库中。
默存
2022/06/24
3.3K1
SpringBoot AOP 记录操作日志、异常日志
日志记录规范总结
然而,日志记录的好坏直接关系到系统出现问题时定位的速度。同时,我们可以通过对日志的观察和分析,提前发现系统可能的风险,避免线上事故的发生。对于服务端开发人员来说,线上日志的监控尤其重要,能够帮助我们第一时间发现线上问题并及时解决。
江不知
2019/12/12
4K0
日志记录规范总结
Redis 错误日志记录
日志描述:(错误)misconf redis被配置以保存数据库快照,但misconf redis目前不能在硬盘上持久化。用来修改数据集合的命令不能用,请使用日志的错误详细信息。
郭顺发
2023/07/07
2.1K0
aop记录登录日志
在网站或者各种软件系统中,多多少少会有一些日志需要我们来记录,从而查看当前系统的运行状态。
时光潜流
2023/10/22
3310
异步记录PHP日志
1, 'message' => '请求成功'); echo json_encode($result); $message = '这是变量内容'; //投递日志开始,只有FPM模块才有 if(function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } sleep(1);//即使加了sleep也不影响原有逻辑执行 file_put_contents('/tmp/log.txt', date('Y-m-d H:i:s'
苦咖啡
2018/04/28
2.1K0

相似问题

如何为@RestController启用日志记录?

22

如何为OpenCV启用日志记录

2115

如何为Spring Security启用日志记录?

640

如何为Apache FtpServer启用日志记录

10

如何为子进程启用日志记录

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文