您的位置:首页 > 移动开发 > Android开发

Android项目-PopupWndow与应用的启动与卸载

2015-11-19 20:31 555 查看

Android项目-PopupWndow与应用的启动与卸载

PopupWindow

我们可以使用一个PopupWindow来显示任何View,可以理解为,一个PopupWindow是一个浮在当前activity上的容器。

下面代码,简单的创建了一个PopupWndow

//创建一个只带窗体的PopupWindow, 当这个PopupWindow显示在Activity上时,它所显示的内容都由contentView提供,
//还是那句话,他就是一个提供界面显示的容器
popupwindow = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
//这个是要让PopupWindow显示的UI界面
View contentView = View.inflate(getApplicationContext(),R.layout.popup_item, null);
//将PopupWndow显示在界面的指定位置:  显示在(parent:父容器)的坐标系下的 x = 60, y=60处
popupwindow.showAtLocation(parent, Gravity.LEFT+ Gravity.TOP, 60, 60);


给PopupWndow的View添加动画

要想使PopupWndow的View显示动画,前提条件是PopupWindow的窗体必须有背景,否则View是不可以播放动画的。

继续上面的代码:

popupwindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //透明的背景
//缩放动画
ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f,1.0f, Animation.RELATIVE_TO_SELF, 0,Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(200);
//透明动画
AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
aa.setDuration(200);
AnimationSet set = new AnimationSet(false);
set.addAnimation(aa);
set.addAnimation(sa);
contentView.startAnimation(set);


PopupWindow显示完之后,必须要dismiss: popupwindow.dismiss(), 不然会留在界面上

应用的启动与卸载

启动

private void startApplication() {
// 打开这个应用程序的入口activity。
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(AppInfo.getPackname());
if (intent != null) {
startActivity(intent);
} else {
Toast.makeText(this, "该应用没有启动界面", Toast.LENGTH_SHORT).show();
}
}


卸载

private void uninstallApplication() {

if (AppInfo.isUserApp()) { //卸载用户程序
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + AppInfo.getPackname()));
startActivity(intent);
}else{
//系统应用 ,root权限 利用linux命令删除文件。
if(!RootTools.isRootAvailable()){
Toast.makeText(this, "卸载系统应用,必须要root权限", Toast.LENGTH_SHORT).show();
return ;
}
try {
if(!RootTools.isAccessGiven()){  //判断本应用,是否拥有root权限
Toast.makeText(this, "本应用无root权限", Toast.LENGTH_SHORT).show();
return ;
}
RootTools.sendShell("mount -o remount ,rw /system", 3000);
RootTools.sendShell("rm -r "+AppInfo.getApkpath(), 30000);
} catch (Exception e) {
e.printStackTrace();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android