在觀看最新的Google I/O 2016音頻演示文稿時,我終於找到了導致此問題的原因和(醜陋的)解決方案。
剛纔看這個,你管剪輯的一分鐘左右(在8m56s開始): https://youtu.be/F2ZDp-eNrh4?t=8m56s
它解釋了爲什麼這種情況正在發生以及如何擺脫它。
實際上,Android會在觸摸不活動幾秒鐘後降低CPU速度以減少電池使用量。視頻中的傢伙承諾很快就會有適當的解決方案,但現在唯一能擺脫它的方法就是發送虛假的觸摸(這是官方的建議)。
Instrumentation instr = new Instrumentation();
instr.sendKeyDownUpSync(KeyEvent.KEYCODE_BACKSLASH); // or whatever event you prefer
每1.5秒鐘重複一次這個計時器,問題就會消失。
我知道,這是一個醜陋的黑客,它可能有醜陋的副作用,必須處理。但現在,它只是唯一的解決方案。
更新: 關於您最新的評論...這是我的解決方案。 我在屏幕邊界外的位置使用常規MotionEvent.ACTION_DOWN。其他一切都以不希望的方式干擾了UI。要避免SecurityException,請在主活動的onStart()處理程序中初始化計時器,並在onStop()處理程序中終止它。當應用程序轉到後臺(取決於CPU負載)時,您仍然有可能遇到SecurityException,因此您必須使用try catch塊來包圍假觸摸調用。
請注意,我使用自己的計時器框架,因此您必須轉換代碼以使用任何想要使用的計時器。
此外,我還不能確保代碼是100%防彈。我的應用程序已經應用了該應用程序,但目前處於測試狀態,因此如果此功能在所有設備和Android版本上正常運行,我無法給您任何保證。
Timer fakeTouchTimer = null;
Instrumentation instr;
void initFakeTouchTimer()
{
if (this.fakeTouchTimer != null)
{
if (this.instr == null)
{
this.instr = new Instrumentation();
}
this.fakeTouchTimer.restart();
}
else
{
if (this.instr == null)
{
this.instr = new Instrumentation();
}
this.fakeTouchTimer = new Timer(1500, Thread.MIN_PRIORITY, new TimerTask()
{
@Override
public void execute()
{
if (instr != null && fakeTouchTimer != null && hasWindowFocus())
{
try
{
long downTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, -100, -100, 0);
instr.sendPointerSync(event);
event.recycle();
}
catch (Exception e)
{
}
}
}
}, true/*isInfinite*/);
}
}
void killFakeTouchTimer()
{
if (this.fakeTouchTimer != null)
{
this.fakeTouchTimer.interupt();
this.fakeTouchTimer = null;
this.instr = null;
}
}
@Override
protected void onStop()
{
killFakeTouchTimer();
super.onStop();
.....
}
@Override
protected void onStart()
{
initFakeTouchTimer();
super.onStart();
.....
}
來源
2016-05-23 09:13:27
gal
在運行Android 4.4.2的Nexus 5上也不會發生這種情況。 –
回放期間記錄線程優先級似乎表明線程優先級在點擊屏幕時並未真正改變。也許觸摸事件觸發了一箇中斷,這會擾亂調度程序可能返回的內容? –
我在我的應用程序中遇到同樣的問題。只要我與屏幕互動,所有的運行都應該順利進行。一旦我停止觸摸屏幕,它會在幾秒鐘後(約3-5秒)開始大量丟失。當我再次開始與屏幕交互時,它會恢復平穩運行。不管屏幕交互是否真實,也不管手指是否用特定的控制/按鈕觸發。只要手指在屏幕上移動,所有音頻運行平穩。它不會在Android 5.x上發生,它只是從6.0開始,在Android上也是這樣。N – gal