跑马灯自定义控件 MarqueeView(https://github.com/sunfusheng/MarqueeView
) 继承自ViewFlipper。部分手机屏幕自动锁屏后立即按旁边的开机键,不用解锁就能回到页面,此时再调用startFlipping();不会再轮播。
原因是ViewFlipper监听了锁屏和解锁的广播,在锁屏时将mUserPresent这个属性设置为false,解锁之后重置为true, mUserPresent为true时才会轮播。部分手机由于不用解锁就回到了解锁状态,所以这个值就一直是false.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
mUserPresent = false;
updateRunning();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
mUserPresent = true;
updateRunning(false);
}
}
};
//轮播的方法
private void updateRunning(boolean flipNow) {
boolean running = mVisible && mStarted && mUserPresent;
//mUserPresent为false则停止轮播
if (running != mRunning) {
if (running) {
showOnly(mWhichChild, flipNow);
postDelayed(mFlipRunnable, mFlipInterval);
} else {
removeCallbacks(mFlipRunnable);
}
mRunning = running;
}
if (LOGD) {
Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted
+ ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning);
}
}
解决方法:通过反射将mUserPresent设置为true
public void setUserPresent(boolean userPresent) {
Class clazz = ViewFlipper.class;
try {
Field f = clazz.getDeclaredField("mUserPresent");
f.setAccessible(true); //设置些属性是可以访问的
f.setBoolean(this, userPresent);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public boolean getUserPresent() {
Class clazz = ViewFlipper.class;
try {
Field f = clazz.getDeclaredField("mUserPresent");
f.setAccessible(true); //设置些属性是可以访问的
return f.getBoolean(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
屏幕解锁后判断mUserPresent是否为true,如果不是则设置其为true.
网友评论