PopWindow 需要依赖于window 窗口,也就是需要Activity 对象,所以我创建实例PopWindow的时候必须传入Activity
还要每个 activity 去监听消息才行
private void registerAppMessageBroadcast(){
final Map<Integer,BroadcastReceiver> bcr = new HashMap<Integer, BroadcastReceiver>();
mAppContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityResumed(final Activity activity) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(activity);
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
String icon = intent.getStringExtra(IMExtraName.MSG_ICON);
String title = intent.getStringExtra(IMExtraName.MSG_TITLE);
String msgContent = intent.getStringExtra(IMExtraName.MSG_CONTENT);
String msgUrl = intent.getStringExtra(IMExtraName.MSG_URL);
HeadsUpPopupWindow headsUpPopupWindow = new HeadsUpPopupWindow(activity);
headsUpPopupWindow.showHeadsUp(activity,icon,title,msgContent,msgUrl);
}catch (Throwable e){
}
}
};
bcr.put(activity.hashCode(),broadcastReceiver);
lbm.registerReceiver(broadcastReceiver,new IntentFilter(ImConfig.MESSAGE_REMIND_BROADCAST));
}
@Override
public void onActivityPaused(Activity activity) {
try {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(activity);
BroadcastReceiver broadcastReceiver = bcr.get(activity.hashCode());
if(broadcastReceiver != null){
lbm.unregisterReceiver(broadcastReceiver);
bcr.remove(activity.hashCode());
}else {
//nothing
}
}catch (Throwable e){
}
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
@Override
public void onActivityDestroyed(Activity activity) {}
});
}
public class HeadsUpPopupWindow extends PopupWindow {
private TextView mTitleTv;
private TextView mContentTv;
private ImageView mImageView;
private RelativeLayout windowRL;
private float downY;
public HeadsUpPopupWindow(final Activity activity ) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.heads_up, null);
setContentView(contentView);
this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
// 设置点击窗口外边窗口消失
this.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
this.setFocusable(false);
initView(contentView);
}
private void initView(View contentView ){
mTitleTv = (TextView) contentView.findViewById(R.id.tv1);
mContentTv=(TextView) contentView.findViewById(R.id.tv2);
mImageView = (ImageView) contentView.findViewById(R.id.iv1);
windowRL=(RelativeLayout) contentView.findViewById(R.id.windowRL);
}
public void showHeadsUp(final Activity activity,String icon, String title, String msgContent,final String msgUrl){
windowRL.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
float currentY = event.getRawY();
if((downY - currentY) >= 10 || TextUtils.isEmpty(msgUrl) || !msgUrl.startsWith("http")){
try {
dismiss();
}catch (Throwable e){}
}else {
KidImActivity.startActivity(activity,msgUrl);
}
break;
}
return true;
}
});
mTitleTv.setText(title);
mContentTv.setText(msgContent);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)// 设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_launcher)// 设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
.build();
ImageLoaderUtil.displayImage(icon,mImageView,options);
showAtLocation(new View(activity), Gravity.NO_GRAVITY,0,0);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
dismiss();
}catch (Throwable e){}
}
},5000);
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
super.showAtLocation(parent, gravity, x, y);
}