android批量删除app开发学习

考虑到app批量卸载,每次卸载一个app都会弹出相应卸载界面

如果想在后如直接卸载,而不弹出界面

必须使用shell命令来执行卸载代码,也就是Linux命令


关于Java中使用Shell命令学习:

1,使用ProcessBuilder类来实现Shell命令构建与执行

2,使用ProcessBuilder的start()方法来执行Shell代码

3,执行后就可以得到执行结果InputStream


代码示例:

	private void excShell(String ...args) {
		ProcessBuilder pb = new ProcessBuilder(args);
		
		try {
			Process p = pb.start();
			InputStream inStream = p.getInputStream();
			Log.d(TAG, new String(convertInStream2String(inStream)));
		} catch (IOException e) {
			e.printStackTrace();
			finish();
		}	
	}
	
	byte[] convertInStream2String(InputStream inStream) {
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		int count = 0;
		byte[] buffer = new byte[4096];
		try {
			while ((count = inStream.read(buffer)) > 0) {
				b.write(buffer, 0, count);
			}
		} catch (IOException e) {
			e.printStackTrace();
			finish();
		} finally {
			try {
				if(b != null) {
					b.close();
					b = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return b.toByteArray();
	}

执行结果示例:

excShell("ps");
04-26 10:36:40.690: D/MainActivity(18093): USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
04-26 10:36:40.690: D/MainActivity(18093): root      1     0     488    348   ffffffff 00000000 S /init
04-26 10:36:40.690: D/MainActivity(18093): root      2     0     0      0     ffffffff 00000000 S kthreadd
04-26 10:36:40.690: D/MainActivity(18093): root      3     2     0      0     ffffffff 00000000 S ksoftirqd/0
04-26 10:36:40.690: D/MainActivity(18093): root      6     2     0      0     ffffffff 00000000 S migration/0
04-26 10:36:40.690: D/MainActivity(18093): root      7     2     0      0     ffffffff 00000000 S watchdog/0
04-26 10:36:40.690: D/MainActivity(18093): root      20    2     0      0     ffffffff 00000000 S khelper
04-26 10:36:40.690: D/MainActivity(18093): root      24    2     0      0     ffffffff 00000000 S netns
04-26 10:36:40.690: D/MainActivity(18093): root      28    2     0      0     ffffffff 00000000 S sync_system_wor
04-26 10:36:40.690: D/MainActivity(18093): root      429   2     0      0     ffffffff 00000000 S sync_supers
04-26 10:36:40.690: D/MainActivity(18093): root      431   2     0      0     ffffffff 00000000 S bdi-default
04-26 10:36:40.690: D/MainActivity(18093): root      433   2     0      0     ffffffff 00000000 S kblockd
04-26 10:36:40.690: D/MainActivity(18093): root      452   2     0      0     ffffffff 00000000 S khubd
04-26 10:36:40.690: D/MainActivity(18093): root      472   2     0      0     ffffffff 00000000 S irq/365-max7769
04-26 10:36:40.690: D/MainActivity(18093): root      494   2     0      0     ffffffff 00000000 S irq/359-max7768
04-26 10:36:40.690: D/MainActivity(18093): root      538   2     0      0     ffffffff 00000000 S cfg80211
04-26 10:36:40.690: D/MainActivity(18093): root      625   2     0      0     ffffffff 00000000 S kpegasusq
04-26 10:36:40.690: D/MainActivity(18093): root      655   2     0      0     ffffffff 00000000 S khungtaskd
04-26 10:36:40.690: D/MainActivity(18093): root      656   2     0      0     ffffffff 00000000 S kswapd0
04-26 10:36:40.690: D/MainActivity(18093): root      710   2     0      0     ffffffff 00000000 S fsnotify_mark
04-26 10:36:40.690: D/MainActivity(18093): root      728   2     0      0     ffffffff 00000000 S ecryptfs-kthrea
04-26 10:36:40.690: D/MainActivity(18093): root      732   2     0      0     ffffffff 00000000 S crypto
04-26 10:36:40.690: D/MainActivity(18093): root      745   2     0      0     ffffffff 00000000 S s3c-fb
04-26 10:36:40.690: D/MainActivity(18093): root      751   2     0      0     ffffffff 00000000 S s3c-fb-vsync
04-26 10:36:40.690: D/MainActivity(18093): root      835   2     0      0     ffffffff 00000000 S irq/382-wm8994
04-26 10:36:40.690: D/MainActivity(18093): root      846   2     0      0     ffffffff 00000000 S s3c64xx-spi.1
04-26 10:36:40.690: D/MainActivity(18093): root      887   2     0      0     ffffffff 00000000 S mtp_read_send
04-26 10:36:40.690: D/MainActivity(18093): root      893   2     0      0     ffffffff 00000000 S file-storage
04-26 10:36:40.690: D/MainActivity(18093): root      909   2     0      0     ffffffff 00000000 S irq/387-melfas-
04-26 10:36:40.690: D/MainActivity(18093): root      956   2     0      0     ffffffff 00000000 S s5p-jpeg
04-26 10:36:40.690: D/MainActivity(18093): root      968   2     0      0     ffffffff 00000000 S kfimg2dd
04-26 10:36:40.690: D/MainActivity(18093): root      973   2     0      0     ffffffff 00000000 S mali_dvfs
04-26 10:36:40.690: D/MainActivity(18093): root      976   2     0      0     ffffffff 00000000 S mali-pmm-wq
04-26 10:36:40.690: D/MainActivity(18093): root      979   2     0      0     ffffffff 00000000 S sii9234_msc_wq
04-26 10:36:40.690: D/MainActivity(18093): root      980   2     0      0     ffffffff 00000000 S sii9234_tmds_of
——————————————————————————————————————————————————————————————————————————————————————————————————————

android中批量卸载app,经网络参考

Step1:把要卸载的APK包名保存到一个文件中,本文称之为file,保存格式如下

...



你可能感兴趣的:(android批量删除app开发学习)