这是一个非常老的问题,但是在付出了很多努力之后,我仍然找不到一个可行的解决方案。
目的
其实很简单。我想开发一些全屏应用程序(例如游戏),其中状态栏不会展开,但是用户会触摸屏幕。
该应用程序应该能够在Android 5.0,sdk 21(Lollipop)上运行。
到目前为止我做了什么
我发现这篇文章描述了如何使用CustomView
吸收触摸事件的a来覆盖状态栏,但对我而言不起作用...
我什至更改了的大小CustomView
,使其覆盖了整个屏幕,但仍然无法正常工作,甚至无法覆盖我的按钮。
我使用的代码附在下面。在设备上运行时,按钮和状态栏均起作用-没有任何阻塞。
我想念什么吗?
代号
在AndroidManifest.xml中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在activity_main.xml中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/text_show" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/button_click"
android:onClick="changeText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
在MainActivity.java中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
CustomViewGroup view = new CustomViewGroup(this);
manager.addView(view, localLayoutParams);
setContentView(R.layout.activity_main);
}
boolean key = false;
public void changeText(View view) {
TextView tv = (TextView) findViewById(R.id.text_show);
if (key) {
tv.setText("hahaha");
} else {
tv.setText("eroero");
}
key = ! key;
}
在CustomViewGroup中(从上面的链接复制):
public class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("CustomViewGroup", "**********Intercepted");
return true;
}
}
最后,我能做的最好的就是使用Android 5.0中引入的“任务锁定”功能。
参考:startLockTask。
此方法的问题:
由于我的目的不是使用信息亭模式,因此我并不在乎第2点。
点1也可以接受,但是有点烦人,因为每次onResume
调用都会(并且应该)询问它。
可以通过将服务应用程序设置为设备所有者来解决,该应用程序将权限授予需要它的应用程序。但是用户必须自己做(参见此答案),这是合理的,否则应用程序将能够完全绑架您的设备。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句