android通过toast实现悬浮通知效果,如图:
实现的功能:
代码如下:
import android.content.Context; import android.os.Build; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import cn.droidlover.xdroidmvp.router.Router; import io.slife.wallet.R; import io.slife.wallet.config.IntentKey; import io.slife.wallet.ui.NewsFlashDetailActivity; public class PushToast { private AppCompatActivity mActivity; private static PushToast mInstance; private Toast mToast; private final int SHOW = 1; private final int HIDE = 0; private Object mTN; private Method mShow; private Method mHide; private Field mViewFeild; private long durationTime = 5*1000; public static PushToast getInstance() { if (mInstance == null) { mInstance = new PushToast(); } return mInstance; } public void init(AppCompatActivity activity) { mActivity = activity; } public void createToast(String title, String content, Mapparams) { if (mActivity == null) { return; } LayoutInflater inflater = mActivity.getLayoutInflater();//调用Activity的getLayoutInflater() // LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.view_push_toast, null); //加載layout下的布局 LinearLayout llPushContent = (LinearLayout) view.findViewById(R.id.ll_push_content); TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); TextView tvContent = (TextView) view.findViewById(R.id.tv_content); tvTitle.setText(title); tvContent.setText(content); mToast = new Toast(mActivity); mToast.setView(view); mToast.setDuration(Toast.LENGTH_LONG); mToast.setGravity(Gravity.TOP, 0, 0); reflectEnableClick(); reflectToast(); llPushContent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String newsFlashId = params.get("InformationID"); Router.newIntent(mActivity).to(NewsFlashDetailActivity.class).putString(IntentKey.NEWS_FLASH_ID,newsFlashId).launch(); handler.sendEmptyMessage(HIDE); } }); if(mShow != null && mHide != null){ handler.sendEmptyMessage(SHOW); }else{ mToast.show(); } } private void reflectEnableClick() { try { Object mTN; mTN = getField(mToast, "mTN"); if (mTN != null) { Object mParams = getField(mTN, "mParams"); if (mParams != null && mParams instanceof WindowManager.LayoutParams) { WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams; //显示与隐藏动画 // params.windowAnimations = R.style.ClickToast; //Toast可点击 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; //设置viewgroup宽高 params.width = WindowManager.LayoutParams.MATCH_PARENT; //设置Toast宽度为屏幕宽度 params.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置高度 } } } catch (Exception e) { e.printStackTrace(); } } /** * 反射字段 * * @param object 要反射的对象 * @param fieldName 要反射的字段名称 */ private static Object getField(Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException { Field field = object.getClass().getDeclaredField(fieldName); if (field != null) { field.setAccessible(true); return field.get(object); } return null; } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SHOW: handler.sendEmptyMessageDelayed(HIDE, durationTime); show(); break; case HIDE: hide(); break; } } }; public void reflectToast() { Field field = null; try { field = mToast.getClass().getDeclaredField("mTN"); field.setAccessible(true); mTN = field.get(mToast); mShow = mTN.getClass().getDeclaredMethod("show"); mHide = mTN.getClass().getDeclaredMethod("hide"); mViewFeild = mTN.getClass().getDeclaredField("mNextView"); mViewFeild.setAccessible(true); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } } public void show() { try { //android4.0以上就要以下处理 if (Build.VERSION.SDK_INT > 14) { Field mNextViewField = mTN.getClass().getDeclaredField("mNextView"); mNextViewField.setAccessible(true); LayoutInflater inflate = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = mToast.getView(); mNextViewField.set(mTN, v); Method method = mTN.getClass().getDeclaredMethod("show", null); method.invoke(mTN, null); } mShow.invoke(mTN, null); } catch (Exception e) { e.printStackTrace(); } } private void hide() { try { mHide.invoke(mTN, null); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }catch (NullPointerException ex){ ex.printStackTrace(); } } }
xml布局:
点九格式图片:
使用方法:
activity中需要初始化一次:
PushToast.getInstance().init(this);
调用:
PushToast.getInstance().createToast(msg.title,msg.text,umengPushEntity.getExtraMap());
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔