转载请注明出处:https://blog.csdn.net/mythmayor/article/details/72900287
1.卸载应用的Intent
卸载请求
Intent intent = new Intent();
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:"+clickedAppInfo.getPackName()));
startActivity(intent);
安装请求
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(
Uri.fromFile(new File(apk_path)),
"application/vnd.android.package-archive");
startActivity(intent);
2.接收卸载应用程序的广播
AppUninstallReceiver receiver = new AppUninstallReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
registerReceiver(receiver, filter);
- 相关广播
- ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)
- ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本
- ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被卸载的包程序不能接收到这个广播)
3.遍历集合时如何删除数据
方法一:高级for循环,记录要删除的数据,遍历后再删除
AppInfo deleteAppInfo = null;
//更新ui界面
for(AppInfo appinfo: userAppInfos){
if(appinfo.getPackName().equals(packname)){
deleteAppInfo = appinfo;
}
}
if(deleteAppInfo!=null){
userAppInfos.remove(deleteAppInfo);
}
方法二:使用迭代器进行遍历,可在遍历中删除
Iterator<AppInfo> iterator = userAppInfos.iterator();
while(iterator.hasNext()){
AppInfo appinfo = iterator.next();
if (appinfo.getPackName().equals(packname)) {
iterator.remove();
}
}
方法三:使用普通for循环倒叙,可在遍历中删除。注意:如不采用倒叙,会遍历不全
for (int i = userAppInfos.size() - 1; i >= 0; i--) {
AppInfo appinfo = userAppInfos.get(i);
if (appinfo.getPackName().equals(packname)) {
userAppInfos.remove(i);
break;
}
}
4.启动一个应用程序
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(clickedAppInfo.getPackName());
if(intent!=null){
startActivity(intent);
}else{
Toast.makeText(this, "对不起,该应用无法被开启", 0).show();
}
5.分享
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "推荐你使用一款软件:"+clickedAppInfo.getAppName()+",真的很好用哦");
startActivity(intent);
6.详细信息
Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:"+clickedAppInfo.getPackName()));
startActivity(intent);
7.设备进程信息获取
获取设备运行进程
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcessInfos = am.getRunningAppProcesses();
获取设备RAM
MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
outInfo.availMem;//设备可用RAM
outInfo.totalMem;//设备总RAM
8.获取进程占用RAM
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{info.pid});
long memSize = processMemoryInfo[0].getTotalPrivateDirty()*1024;
9.ListView中Item中有CheckBox、Button等的焦点处理
ListView的item布局中有CheckBox、Button等会获取焦点的控件会抢走焦点,造成ListView的item点击事件相应不了
解决方法:控件设置
android:clickable="false"
android:focusable="false"
或者
android:focusable="false"
//descendant 后裔; 后代; (由过去类似物发展来的) 派生物; 弟子;
android:descendantFocusability="blocksDescendants"
10.ListView中getItemAtPosition()的使用
ListView的getItemAtPosition()方法的返回值即为adapter中getItem()方法返回的对象
11.清理进程
1.添加权限
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
2.具体实现
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.killBackgroundProcesses(String pkgName);