protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_manager);
initView();
initData();
// 注册卸载的广播接收者
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);//行动,包,删除
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);//优先级高
filter.addDataScheme("package"); // 这个方法一定要调用, 否则收不到广播
registerReceiver(mUninstallReceiver, filter );
}
@Override
public void onReceive(Context context, Intent intent) {
String dataString = intent.getDataString();
System.out.println("data:"+dataString);
// package:com.itheima.mobilesafe79
String packageName = dataString.replace("package:", "");
// 遍历过程中移除
ListIterator<AppInfo> listIterator = mUserDatas.listIterator();
while (listIterator.hasNext()) {
AppInfo appInfo = listIterator.next();
if(TextUtils.equals(appInfo.mPackageName, packageName)) {
listIterator.remove();
}
}
// 刷新ListView
mAdapter.notifyDataSetChanged();
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mUninstallReceiver);
}
private void showPopupWindow(View view) {//传入view是要显示在此view的相对位置
if (mPopupWindow == null) {
int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
View contentView = View
.inflate(getApplicationContext(), R.layout.view_pop_window, null);
TextView tvUninstall = (TextView) contentView.findViewById(R.id.tv_pop_uninstall);
TextView tvOpen = (TextView) contentView.findViewById(R.id.tv_pop_open);
TextView tvShare = (TextView) contentView.findViewById(R.id.tv_pop_share);
TextView tvInfo = (TextView) contentView.findViewById(R.id.tv_pop_info);
tvUninstall.setOnClickListener(this);
tvOpen.setOnClickListener(this);
tvShare.setOnClickListener(this);
tvInfo.setOnClickListener(this);
PopupWindow mPopupWindow = new PopupWindow(contentView, width, height);
// 表示能获取焦点
mPopupWindow.setFocusable(true);
// 设置背景, 必须设置背景才能在点击别的位置时消失, 或者按返回键消失
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 设置动画的样式, 主要是改 Interpolator
mPopupWindow.setAnimationStyle(R.style.PopWindowAnimation);
}
// anchor: 锚点, View, popupWindow显示出来的时候显示在这个View的下面
mPopupWindow.showAsDropDown(view, 60, -view.getHeight()); // 后面两个参数是x,y轴的偏移量
}