gpt4 book ai didi

android - 发生特定事件时如何在 Android 上打开屏幕?

转载 作者:行者123 更新时间:2023-11-30 02:51:53 25 4
gpt4 key购买 nike

我正在尝试创建一个闹钟应用程序,它应该在给定的时间段后响起。它在屏幕打开时工作;我打开应用程序,闹钟在 5 秒后响起。这正是我想要的。

现在,我希望我的应用在屏幕关闭时以相同的方式运行。所以,如果我打开我的应用程序然后关闭屏幕,我希望应用程序在 5 秒后打开屏幕,然后响铃。

我尝试了 SCREEN_BRIGHT_WAKE_LOCK(确实设置了所需的权限)但它没有用。闹钟响了半秒钟,然后就关掉了。屏幕根本没有亮起。此外,SCREEN_BRIGHT_WAKE_LOCK 已被弃用,文档建议使用 FLAG_KEEP_SCREEN_ON,这仅在屏幕已经打开并且我们希望保持这种状态时才有用。不要从关闭状态打开它。

我需要做什么来解决上述问题?使用服务?此外,屏幕被数字锁锁定。

这是我的代码,它仅在屏幕打开时有效:

public class AlarmReceiverActivity extends Activity {



private MediaPlayer mMediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_alarm_receiver);

Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
mMediaPlayer.stop();
mMediaPlayer.release();
finish();
return false;
}
});

playSound(this, getAlarmUri());
}

private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

try {
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
System.out.println("Oops");
}

}
} catch (IOException e1) {
System.out.println("OOPS");
}
}

//Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
private Uri getAlarmUri() {
Uri alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
}

最佳答案

我最近正在处理这个问题。这对我有用:

public class YourActivity extends Activity {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

...

}

有用的链接:

Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling

http://weimenglee.blogspot.co.il/2013/04/android-tip-waking-up-screen-and.html

Android Galaxy S4 -- Activity that is visible over lock screen

也不要在 Activity 中使用对话框主题!!!

关于android - 发生特定事件时如何在 Android 上打开屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com