Android开发之做一键批量卸载App功能的详细讲解

上传人:m**** 文档编号:61964156 上传时间:2018-12-15 格式:DOCX 页数:11 大小:44.73KB
返回 下载 相关 举报
Android开发之做一键批量卸载App功能的详细讲解_第1页
第1页 / 共11页
Android开发之做一键批量卸载App功能的详细讲解_第2页
第2页 / 共11页
Android开发之做一键批量卸载App功能的详细讲解_第3页
第3页 / 共11页
Android开发之做一键批量卸载App功能的详细讲解_第4页
第4页 / 共11页
Android开发之做一键批量卸载App功能的详细讲解_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《Android开发之做一键批量卸载App功能的详细讲解》由会员分享,可在线阅读,更多相关《Android开发之做一键批量卸载App功能的详细讲解(11页珍藏版)》请在金锄头文库上搜索。

1、Android开发之做一键批量卸载App功能的详细讲解首先准备一部已经Root的手机,然后打开AndroidStudio,下面我们开始快乐的写代码吧首先我们先分析具体的业务需求:很简单的一个需求,最主要的功能就是可以卸载App;同时要求可以批量卸载;既然能够批量卸载,也就是说我们在UI交互上可以批量选择;能大量展示待卸载的App。好的我们现在一步一步的来:首先我们先解决最主要的需求,卸载App!有两种方式可以实现App卸载:分为静默方式和非静默方式。什么是静默方式?意思就是说卸载完全是在系统后台进行的,不需要用户去点击确认卸载。非静默方式的意思显而易见,卸载的时候需要用户点击确认,只有用户确认

2、卸载才会卸载。我们先说非静默方式卸载:非静默方式卸载的代码如下;?123456789public void unstallApp(String pageName)Intent uninstallIntent = new Intent();uninstallIntent.setAction(Intent.ACTION_DELETE);uninstallIntent.setData(Uri.parse(package:+pageName);startActivityForResult(uninstall_intent,1);从代码中我们就可以看出来,这里开启了一个活动,也就是所谓的应用卸载程序,然

3、后把需要卸载的App包名交给它,它就会把这个App给卸载掉。这是正常的App卸载步骤。开启这个应用卸载程序活动后,页面就会跳转到卸载页面,然后等待用户点击确定或者取消,点击确定就会执行卸载程序,点击取消就会回退到原来的活动。在这里我们使用了startActivityForResult()方法来开启应用卸载活动,目的是为了卸载完成后在回掉函数里面可以更新原来的App列表页面。非静默方式代码非常的简单,也非常容易理解,但是这里有个不足之处,那就是如果我们一次性需要卸载十个APP应用,那么页面将会跳转十次,同时你也需要点击十次确定!别忘了我们这里可是要求批量卸载,如果让用户去连续点击十次确定,这样会

4、非常影响用户体验!所以非静默方式卸载在这里使用并不是很好,静默方式是更好的选择!静默方式卸载:静默方式也就是意味着我们需要绕过安卓的界面,在后台执行卸载命令,那么怎么做呢?很显然,当然是使用命令了!使用命令的方式我们可以绕过安卓界面执行。这里有两种卸载App命令:首先是adb命令:adbuninstall还有一个pm命令:pm uninstall我们可以看到这两种命令写法相同,命令的开头不同,那么他们具体的差别在什么地方呢?应该用哪一种命令方式?还是两种命令方式都合适呢?我先不说区别,我们去实地的测试一下,首先我们先用adb命令去卸载。代码如下:?12345678910111213141516

5、171819202122232425262728293031323334353637383940414243444546474849505152package com.example.uninstallapk;import android.util.Log;import java.io.DataOutputStream;/* Created by 王将 on 2018/7/23.*/adb命令翻译执行类public class RootCmd /* param command* return*/public static boolean exusecmd(String command) Pro

6、cess process = null;DataOutputStream os = null;try process = Runtime.getRuntime().exec(su);os = new DataOutputStream(process.getOutputStream();os.writeBytes(command + n);os.writeBytes(exitn);os.flush();Log.e(updateFile, =000=writeSuccess=);process.waitFor(); catch (Exception e) Log.e(updateFile, =11

7、1=writeError= + e.toString();return false; finally try if (os != null) os.close();if (process != null) process.destroy(); catch (Exception e) e.printStackTrace();return true;public static void unInstallApk(String pageName)exusecmd(adb uninstall +pageName);主活动中我们调用:?1RootCmd.unInstallApk(com.example.

8、tset);把想要卸载的App包名传进去,运行一下,很快你就发现:整个应用崩溃了,出现了ANR问题,应用无反应。好,我们改为pm命令试一下,结果发现成功了!那么现在我们分析一下为什么adb命令会导致出现ANR问题,而pm命令就不会出现错误。乐淘棋牌http:/一个命令的下达,肯定会调用相应的方法去处理,只不过这个调用过程在系统的内部,我们外界是看不到的,只能得到命令执行的结果。就好比我们使用命令去卸载App应用,同样也是在内部调用了卸载方法,那么具体这个方法是什么?在哪里呢?下面我们就去深入的探讨一下。Android系统卸载App应用都是调用了一个类 中方法,不管是非静默模式还是静默模式,这个

9、类就是PackageInstaller类。当然Android系统安装App也同样是调用的它里面的方法,这个类功能从它的名字上就可以看出来:打包安装程序。当然这个类我们在平常的开发中是用不到的,同样也是无法调用的,这个类同样也是一个底层调用的类。在这个类中我们可以找到具体的卸载App方法,让我们看一下源码:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172/* Uninstal

10、l the given package, removing it completely from the device. This* method is only available to the current installer of record for the* package.* param packageName The package to uninstall.* param statusReceiver Where to deliver the result.*/public void uninstall(NonNull String packageName, NonNull

11、IntentSender statusReceiver) uninstall(packageName, 0 /*flags*/, statusReceiver);/* Uninstall the given package, removing it completely from the device. This* method is only available to the current installer of record for the* package.* param packageName The package to uninstall.* param flags Flags

12、 for uninstall.* param statusReceiver Where to deliver the result.* hide*/public void uninstall(NonNull String packageName, DeleteFlags int flags,NonNull IntentSender statusReceiver) uninstall(new VersionedPackage(packageName, PackageManager.VERSION_CODE_HIGHEST),flags, statusReceiver);/* Uninstall

13、the given package with a specific version code, removing it* completely from the device. This method is only available to the current* installer of record for the package. If the version code of the package* does not match the one passed in the versioned package argument this* method is a no-op. Use link PackageManager#VERSION_CODE_HIGHEST to* uninstall the latest version of the package.* param versionedPackage The versioned package to uninstall.* param statusReceiver Where to deliver the result.*/public void uninstall(NonNull VersionedPackage versionedPackage,NonNull IntentSende

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 手机/mobile开发

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号