最近在瞭解Window及WindowManger,
Window表示一個窗口,是一個抽象類,具體由PhoneWindow實現。
而創建一個Window需要用到WindowManager, WindowManagerImpl,WindowManagerGlobal, ViewRootImpl等等,
最終以View的形式展現給用戶。具體概念不做介紹,分享一個簡單的例子及遇到的問題以及解決的辦法。
解決遇到問題的過程比較重要。
最初是想在一個Activity 裏通過WindowManager添加一個View, 例子也是書上的
但是結果FC了, 原因如下
09-14 11:11:37.755 9367 9367 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XX.viewmotiontest02/com.XX.viewmotiontest02.ViewMotionTest02}: android.view.WindowManager$InvalidDisplayException: Unable to add window <a target=_blank href="mailto:android.view.ViewRootImpl$W@259f2ae">android.view.ViewRootImpl$W@259f2ae</a> -- the specified window type is not valid
......
09-14 11:11:37.755 9367 9367 E AndroidRuntime: Caused by:<span style="color:#ff0000;"> android.view.WindowManager$InvalidDisplayException: Unable to add window </span><a target=_blank href="mailto:android.view.ViewRootImpl$W@259f2ae"><span style="color:#ff0000;">android.view.ViewRootImpl$W@259f2ae</span></a><span style="color:#ff0000;"> -- the specified window type is not valid</span>
09-14 11:11:37.755 9367 9367 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:817)
對這種錯誤毫無頭緒,並且是書上的例子。。
百度及谷歌了都沒有類似的問題, 嘗試新啓動一個Service, 然後在Service 裏面再添加一個View, 依然保持同樣的錯誤。
準備放棄的時候,去官方API 瞄了一眼 WindowManager$InvalidDisplayException ,介紹說這個Exception是因爲無法作爲第二個View顯示之類的。。
又看看錯誤 是 window type not valid, 想到了每添加一個View , 都會爲其設置WindowManager.LayoutParms, 其中WindowManager.LayoutParms 就有type屬性。。
而看了創建LayoutParms的代碼:
mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
<span style="color:#ff0000;">0</span> , 0, PixelFormat.TRANSPARENT); // <span style="color:#ff0000;">w, h, type, flags, format;</span>
從API上看,LayoutParms對應上面的構造函數中, type 並沒有0這個值的!!!
type: 1~99 應用Window層
type: 1000 ~1999 子Window
type: 2000~2999 系統層 (最頂層)
因此type 爲0 必須報錯!!!!
改成 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 並且添加 uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
所以,關鍵時候,官方API 還是很有幫助的!!!
部分代碼:
package com.XX.viewmotiontest02;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class ViewMotionTest02 extends Activity {
private static final String LOG_TAG = "ViewMotionTest02";
private boolean mIsUseService;
Button mStartBtn;
Button mFloatingButton;
WindowManager.LayoutParams mLayoutParams;
WindowManager mWindowManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG,"onCreate ");
setContentView(R.layout.activity_view_motion_test02);
mIsUseService = false; // true: Service, false: Activity
Log.d(LOG_TAG,"mIsUseService: " + mIsUseService);
if(mIsUseService) {
mStartBtn = (Button) findViewById(R.id.startServiceBtn);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "onClick");
Intent intent = new Intent(getApplicationContext(), ViewMotionService.class);
startService(intent);
}
});
} else {
//mStartBtn.setVisibility(View.INVISIBLE);
mFloatingButton = new Button(this);
mFloatingButton.setText("Button In Activity");
mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 0, PixelFormat.TRANSPARENT); // w, h, type, flags, format;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mLayoutParams.x = 100;
mLayoutParams.y = 300;
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(mFloatingButton,mLayoutParams);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG,"onDestroy ");
if(mIsUseService) {
Intent intent = new Intent(getApplicationContext(), ViewMotionService.class);
stopService(intent);
} else{
mWindowManager.removeView(mFloatingButton);
}
}
}
package com.XX.viewmotiontest02;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Button;
public class ViewMotionService extends Service {
private static final String LOG_TAG = "ViewMotionService";
Button mFloatingButton;
WindowManager.LayoutParams mLayoutParams;
WindowManager mWindowManager;
@Override
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "onCreate" );
mFloatingButton = new Button(this);
mFloatingButton.setText("Button");
mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 0, PixelFormat.TRANSPARENT); // w, h, type, flags, format;
//mLayoutParams = new WindowManager.LayoutParams();
//mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
//mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
//mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
//mLayoutParams.format = PixelFormat.TRANSPARENT;
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mLayoutParams.x = 100;
mLayoutParams.y = 300;
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//Log.d(LOG_TAG,"onCreate - 2 change -3 ");
mWindowManager.addView(mFloatingButton,mLayoutParams);
//Log.d(LOG_TAG,"onCreate - 3 ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy" );
mWindowManager.removeView(mFloatingButton);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.viewmotiontest02">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ViewMotionTest02">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ViewMotionService" >
</service>
</application>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" >
</uses-permission>
</manifest>
---