【发布时间】:2014-11-17 06:24:07
【问题描述】:
我正在制作闹钟。作为其中的一部分,我有一个IntentService,它会在闹钟实际响起时启动一项活动。在 Activity 的onCreate 中,我正在唤醒屏幕,获得唤醒锁,强制 Activity 全屏,并播放声音。这是 onCreate 中的所有内容:
super.onCreate(savedInstanceState);
// Get Alarm ID from the extras
Bundle extras = getIntent().getExtras();
int id = extras.getInt("AlarmID", -1);
// Get Alarm info from the DB
DB = new DatabaseHelper(this);
alarm = DB.getAlarm(id);
if (alarm == null || !alarm.isEnabled()) finish();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_alarm);
// My root view
View contentView = findViewById(R.id.fullscreen_content);
// Hide action bar for full screen
ActionBar bar = getActionBar();
if (bar != null) bar.hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Hide nav bar
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider.hide();
// Show over lock screen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// Wake up screen
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLock");
wakeLock.acquire();
// Get UI Elements
TextView time = (TextView)findViewById(R.id.Time);
TextView name = (TextView)findViewById(R.id.SmallAlarmName);
// Fill UI Elements
time.setText(Alarm.FormatTime(alarm.getHour(), alarm.getMinute()));
name.setText(alarm.getName());
// Play selected ringtone
tone = RingtoneManager.getRingtone(this, alarm.getSound());
tone.setStreamType(RingtoneManager.TYPE_ALARM);
tone.play();
视图相当简单:2x TextViews 显示时间和闹钟名称,2x 可点击 ImageViews 用于贪睡和禁用。
我遇到的问题是我第一次触摸屏幕时,什么也没有发生。两个 ImageView 都以 Log.i 开头,所以我知道如果事件触发,我什么时候推送它。第一次推送时,没有日志输出。在第二次推送时,会触发正确的 ImageView 事件。创建活动时屏幕最初是打开还是关闭都没有关系,第一次触摸会被非常重复地忽略。发生了什么,我该如何解决,以便第一次触摸按预期工作?
我正在我的 Nexus 5、4.4.3、Rooted、stock rom、Xposed Framework 上进行测试(但没有任何模块会影响我的应用)。由于需要使用铃声(VM 没有铃声),我无法真正在虚拟机中进行测试。
此外,为了记录,第一次点击不会发生其他日志。我不知道手机认为我在窃听什么。
编辑:我认为这与标志有关。我注意到我在调用getWindow().setFlags(...) 时调用的是setFlags 而不是addFlags。相反,我将其更改为 addFlags 并尝试将其更改为:
int flags = WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
getWindow().addFlags(flags);
但没有变化。
有什么方法可以让我判断第一次点击可能是什么?
我尝试在 onCreate 结束时设置一个断点并不断跨步,直到它最终等待事件发生,但即使在这种状态下,第一次点击也不会在调试器中进行更改。正如预期的那样,第二次点击我的 ImageView 将在我的点击处理程序内的第一行停止调试器。
编辑 2:我想我正在做某事。我正在通过过滤器观看 logcat,只看到与我的应用程序相关的内容。我尝试在没有过滤器的情况下查看 logcat,并且能够在第一次点击时可靠地获取这些类型的消息:
06-12 23:28:04.437 812-844/? W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
第一次点击会响应其中两个警告。第一次之后的额外点击不会给出这样的警告,我的处理程序将按预期执行。
快速搜索返回 this question,但对我的情况没有帮助。不仅仅是一个警告,我认为这可能是第一次点击被忽略的根本原因。但是,我仍然不知道如何解决它。
【问题讨论】:
-
也许您想展示触摸侦听器的初始化位置和方式以及它们如何处理触摸事件?
-
通过 XML 中的
android:onClick="Snooze_Click"和android:onClick="Disable_Click"进行布局(直接在 ImageView 标签内)。我通过静音和更新警报来处理点击。第一次点击绝对没有任何反应。
标签: android