下面是简单实例:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic);
builder.setTitle("标题");
builder.setMessage("消息");
builder.setPositiveButton(R.string.btn_update, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//增加按钮,回调事件
}
);
builder.setCancelable(false);//弹出框不可以换返回键取消
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//将弹出框设置为全局
dialog.setCanceledOnTouchOutside(false);//失去焦点不会消失
dialog.show();
通过Context.getSystemService(Context.WINDOW_SERVICE)
可以获得WindowManager对象。
每一个WindowManager对象都和一个特定的Display绑定。
想要获取一个不同的display的WindowManager,可以用 createDisplayContext(Display)
来获取那个display的Context,之后再使用:Context.getSystemService(Context.WINDOW_SERVICE)
来获取WindowManager。
调用的是WindowManager继承自基类的addView方法和removeView方法来显示和隐藏窗口。
下面是简单实例:
package com.mg.view.manager;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;
import com.mg.common.dispath.CommonAdvController;
public abstract class BaseWindowManager {
public WindowManager manager;
public Context context;
public LayoutParams params;
public int PARAMS_TYPE;
public boolean isFinish = false;
public static Map showViewMap = new HashMap();
public String imagePath = "";
public Bitmap bitmap, bitmapCorner;
public FrameLayout mContainer;
public BaseWindowManager(Context context) {
this.context = context;
if (android.os.Build.VERSION.SDK_INT >= 19) {
PARAMS_TYPE = LayoutParams.TYPE_TOAST;
} else {
PARAMS_TYPE = LayoutParams.TYPE_PHONE;
}
if (manager == null) {
manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
if (params == null) {
params = new LayoutParams();
switch (getManagerType()) {
case fallType:
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.TOP | Gravity.LEFT;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Translucent;
break;
case floatType:
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.WRAP_CONTENT;
params.flags = LayoutParams.FLAG_FULLSCREEN | LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Translucent;
break;
case coverType:
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Translucent;
break;
default:
break;
}
params.type = PARAMS_TYPE;
}
}
/**
* 显示View
*
*/
public void onShowView(Intent intent) {
CommonAdvController.getInstance(context).onStart();
isFinish = false;
}
/**
* 获取加入WindowManager的view
*
* @return
*/
public abstract View getShowView();
/**
*
*/
public void finish() {
onAdvFinish();
if (bitmapCorner != null) {
bitmapCorner.recycle();
bitmapCorner = null;
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
View view = (View) getShowView();
if (view != null && showViewMap.containsKey(view)) {
try {
manager.removeViewImmediate(view);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
showViewMap.remove(view);
view = null;
}
clearManagerAllView();
System.gc();
}
/**
* 清楚所有managerview
*/
public synchronized void clearManagerAllView() {
if (!showViewMap.isEmpty() && showViewMap.size() > 0) {
for (View baseView : showViewMap.keySet()) {
if (baseView != null) {
manager.removeViewImmediate(baseView);
}
}
showViewMap.clear();
}
}
/**
* 结束显示
*
* @param intent
*/
public void onAdvFinish() {
CommonAdvController.getInstance(context).onStop();
}
/**
* @author ChenDs floatType:浮窗类型 coverType:覆盖类型 fallType:下坠类型
*/
public enum ManagerType {
floatType, coverType, fallType
}
public abstract ManagerType getManagerType();
}
package com.mg.view.utils;
import android.content.Context;
import android.content.Intent;
import com.mg.view.ui.BaseUiActivity;
public class IntentUtils {
/**
* 获取一个带baseui的intent
*
* @param Context
* @return
*/
public static Intent getBaseUiIntent(Context Context, Class> cls) {
Intent intent = new Intent(Context, BaseUiActivity.class);
intent.putExtra(BaseUiActivity.EXTRA_KEY_UI_CLASS, cls.getName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
return intent;
}
}
activity具体封装代码样例:
package com.mg.view.ui;
import java.lang.reflect.Constructor;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import com.mg.common.dispath.CommonAdvController;
import com.mg.view.manager.ActivityManager;
public class BaseUiActivity extends Activity {
public static String EXTRA_KEY_UI_CLASS = new String(new byte[] { 99, 108, 97, 115, 115, 86, 105, 101, 119, 78, 97, 109, 101 }); // classViewName
private BaseUiView mBaseUIVIew;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
// 反射创建对象
try {
Class> classType = Class.forName(intent.getStringExtra(EXTRA_KEY_UI_CLASS));
Constructor> con = classType.getConstructor(Context.class, Intent.class);
mBaseUIVIew = (BaseUiView) con.newInstance(this, intent);
} catch (Exception e) {
e.printStackTrace();
}
if (mBaseUIVIew == null) {
finish();
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
if (mBaseUIVIew.setFullScreenSwitch()) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(mBaseUIVIew);
ActivityManager.getManager().addActivity(this);
mBaseUIVIew.getIntentData(getIntent());
mBaseUIVIew.onCreate();
}
}
@Override
protected void onStart() {
super.onStart();
CommonAdvController.getInstance(this).onStart();
if (mBaseUIVIew != null) {
mBaseUIVIew.onStart();
}
}
@Override
protected void onResume() {
super.onResume();
if (mBaseUIVIew != null) {
mBaseUIVIew.onResume();
}
}
@Override
protected void onPause() {
super.onPause();
if (mBaseUIVIew != null) {
mBaseUIVIew.onPause();
}
}
@Override
protected void onStop() {
super.onStop();
CommonAdvController.getInstance(this).onStop();
if (mBaseUIVIew != null) {
mBaseUIVIew.onStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBaseUIVIew != null) {
mBaseUIVIew.onDestroy();
}
if (ActivityManager.getManager().hasActivity()) {
ActivityManager.getManager().removeCurrentActivityStack();
}
System.gc();
mBaseUIVIew = null;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mBaseUIVIew != null) {
mBaseUIVIew.onNewIntent(intent);
}
}
@Override
public void onBackPressed() {
if (mBaseUIVIew != null && mBaseUIVIew.setBackPressedSwitch()) {
super.onBackPressed();
return;
}
if (mBaseUIVIew != null) {
mBaseUIVIew.onBackPressed();
}
}
@Override
public void finish() {
super.finish();
}
}
转载请说明出处:http://blog.csdn.net/u012438830/article/details/78451575