├── .gitignore
├── .idea
├── compiler.xml
├── misc.xml
├── uiDesigner.xml
└── workspace.xml
├── AndroidAPP.jar
├── AndroidAPP.png
├── GUIConSoleLog.png
├── README.md
├── pom.xml
├── src
└── main
│ └── java
│ ├── DevicesTopAPP.java
│ ├── MainUITest.java
│ ├── ScreenshotUtil.java
│ ├── StreamCaptureThread.java
│ ├── TopAPPInfo.java
│ ├── deviceInfo.java
│ └── getDevices.java
├── target
├── MobileDevicesTools-1.0-SNAPSHOT-jar-with-dependencies.jar
├── MobileDevicesTools-1.0-SNAPSHOT.jar
├── maven-archiver
│ └── pom.properties
└── maven-status
│ └── maven-compiler-plugin
│ ├── compile
│ └── default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └── default-testCompile
│ └── inputFiles.lst
└── 新增支持多设备选择功能.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.war
15 | *.nar
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 | System.out
111 | System.ou
112 | System.out.println
113 | System.out.println(
114 | .getPIDPackName()
115 | .setPIDPackName(
116 | PIDPackName
117 | TopAPPInfo.setPIDPackageName
118 | SetPID(
119 | GetAPPPID(
120 | GetPIDPackageName(
121 | GetPIDPackageNameThread
122 | CmdPull
123 | Main.CmdPull
124 | MainUI
125 | DevicesInfo
126 | TopAPPInfo.getAdbDevices()
127 | DevicesTopAPP.GetDevicesStatus();
128 | GetDevicesStatus
129 | GetAdbDevicesThread
130 | GetPackNameResultThread
131 | GetPackNameResult
132 | Exce
133 | GetTopAPPMainActivity(
134 | GetSystemResult
135 | GetSystemResultThread
136 | GetPIDPackageName
137 | GetmCurrentFocus
138 | GetTopAPPBitThread
139 | GetAppInfo
140 |
141 |
142 |
143 | CmdPull
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 | true
166 | DEFINITION_ORDER
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 | 1543477387228
365 |
366 |
367 | 1543477387228
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 | No facets are configured
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 | 1.8
886 |
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
895 |
896 |
897 | MobileDevicesTools
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
--------------------------------------------------------------------------------
/AndroidAPP.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/AndroidAPP.jar
--------------------------------------------------------------------------------
/AndroidAPP.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/AndroidAPP.png
--------------------------------------------------------------------------------
/GUIConSoleLog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/GUIConSoleLog.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # AndroidDevicesInfo
4 |
5 |
6 | #### Java GUI工具 - 获取Android设备最顶层应用程序相关信息,并提供备份功能;
7 |
8 |
9 |
10 | 简介:
11 |
12 | 该程序通过Java执行ADB CMD命名获取回调信息判断设备信息,获取信息时通过多线程操作节约读取时间;
13 |
14 | 1、获取Android屏幕顶层焦点应用名称,可用于辨别Activity和悬浮窗页面
15 |
16 | 2、获取Android屏幕顶层应用,不能识别悬浮窗
17 |
18 | 3、获取应用程序位数 PackBit
19 |
20 | 4、获取屏幕顶层应用程序常用信息:PackName、Launchable_Activity、TopActivity、versionName、versionCode等
21 |
22 | 5、提供常用APP功能操作:备份、清除缓存、关闭、截图、卸载功能
23 |
24 |
25 | #### 项目详细描述请转移至:https://testerhome.com/opensource_projects/android-app-testtools
26 |
27 |
28 |
29 | --------------------------------------------------
30 |
31 |
32 |
33 | ## 查看当前APP第一界面程序报名和Activity,有坑32位CPU设备Java调用时不支持head命令
34 | $ adb shell dumpsys activity activities |grep -i hist |head -1
35 |
36 | * Hist #0: ActivityRecord{c08fc2a u0 com.test.benjamin/.ui.activity.MainTabActivity t2179}
37 |
38 |
39 | ## 通过包名检查应用程序的启动Activity
40 | $ adb shell dumpsys package com.test.benjamin |grep -B5 android.intent.category.LAUNCHER |grep filter
41 |
42 | 9cd481a com.test.benjamin/.SplashActivity filter f3f0a77
43 |
44 |
45 |
46 | ## 查看系统进程pid,判断运行程序位数,有坑部分机型 adb shell ps 可能显示显示不出进程信息
47 | $ adb shell ps -A |grep com.test.benjamin
48 |
49 | u0_a862 8771 632 2065724 158884 0 0 S com.test.benjamin
50 |
51 | u0_a862 8873 632 1888260 86208 0 0 S com.test.benjamin:pushcore
52 |
53 | u0_a862 9236 632 1852636 85000 0 0 S com.test.benjamin:monitorService
54 |
55 | u0_a862 9433 632 1769844 73896 0 0 S com.test.benjamin:channel
56 |
57 |
58 | ## 查看系统32位 64位进程pid用户对比判断被检查app运行位数
59 | $ adb shell ps -A |grep zygote
60 |
61 | root 631 1 2183544 28908 0 0 S zygote64
62 |
63 | root 632 1 1621752 23992 0 0 S zygote
64 |
65 | webview_zygote 2217 1 1429812 13896 0 0 S webview_zygote32
66 |
67 |
68 |
69 | ## 备份apk
70 | adb shell pm path
71 |
72 | adb pull
73 |
74 |
75 |
76 | ## 清除缓存并重新启动
77 | adb shell pm clean
78 |
79 | adb shell am start -n
80 |
81 |
82 |
83 | ## 关闭程序
84 | adb shell am force-stop
85 |
86 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | MobileDevicesTools
8 | MobileDevicesTools
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.apache.maven.plugins
38 | maven-assembly-plugin
39 | 2.5.5
40 |
41 |
42 |
43 |
44 | MainUITest
45 |
46 |
47 |
48 | jar-with-dependencies
49 |
50 |
51 |
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-compiler-plugin
56 |
57 | 6
58 | 6
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/src/main/java/DevicesTopAPP.java:
--------------------------------------------------------------------------------
1 | import java.util.regex.Matcher;
2 | import java.util.regex.Pattern;
3 |
4 |
5 | /**
6 | * Created by Benjamin on 2018/12/4.
7 | */
8 |
9 | public class DevicesTopAPP {
10 |
11 | public static String GetAdbDevices = "adb devices";
12 | public static String GetTopAPP = "adb shell dumpsys activity activities |grep -i hist |grep visible=true -1";
13 | public static String PackName,PackPath,PackMainActivity;
14 | public static String CompatibleOS = "";
15 | public static String GetTopAPPMainActivity = "adb shell dumpsys package " + PackName + " |grep -B5 android.intent.category.LAUNCHER |grep filter";
16 | public static String GetAppInfo = "adb shell dumpsys package " + PackName + "|grep version";
17 | public static String GetAPPPID = "adb shell ps " + CompatibleOS + " |grep "+ PackName ;
18 | public static String GetSystemPID = "adb shell ps " + CompatibleOS + " |grep zygote64|head -1";
19 | public static String GetPmPath = "adb shell pm path " + PackName;
20 | public static String GetPullFile = "adb pull " + PackPath + " "+ PackName + ".apk";
21 | public static String CloseAPP = "adb shell am force-stop" + PackName;
22 | public static String CleanAPP = "adb shell pm clear " + PackName;
23 | public static String UninstallAPP = "adb uninstall" + PackName;
24 | public static String StartAPP = "adb shell am start -n" + PackMainActivity;
25 | private static String SystemResult;
26 | private static String mCurrentFocus = "adb shell dumpsys window | grep mCurrentFocus";
27 |
28 |
29 | // 执行cmd命令,并回调返回信息
30 | public static String CmdPull(String command){
31 | // 执行命令
32 | // String command = "adb shell getprop ro.product.brand";
33 |
34 | // java虚拟机加载的时候没有adb相关的环境变量,如果执行adb的命令,建议在设定命令的参数里面使用全路径
35 | // mac电脑电脑允许无法获取本地adb环境变量配置,需要手动配置
36 | // command = "/Users/benjamin/Public/public/android-sdk-macosx/platform-tools/" + command;
37 |
38 | try {
39 | Process p = Runtime.getRuntime().exec(command);
40 | StreamCaptureThread errorStream = new StreamCaptureThread(p.getErrorStream());
41 | StreamCaptureThread outputStream = new StreamCaptureThread(p.getInputStream());
42 | new Thread(errorStream).start();
43 | new Thread(outputStream).start();
44 | p.waitFor();
45 | String result = outputStream.output.toString() + errorStream.output.toString();
46 | return result;
47 | } catch (Exception e) {
48 | e.printStackTrace();
49 | return "NaN";
50 | }
51 |
52 | }
53 |
54 | // 获取顶层APP的launcher信息
55 | public static String GetTopAPPMainActivity(String PackName){
56 | GetTopAPPMainActivity = "adb "+ deviceInfo.getSelectDevice() +" shell dumpsys package " + PackName + " |grep -B5 android.intent.category.LAUNCHER |grep filter";
57 | return GetTopAPPMainActivity;
58 | }
59 |
60 | // 设置顶层APP的launcher_activity
61 | public static String SetTopAPPMainActivity(String GetTopAPPMainActivity){
62 | try {
63 | GetTopAPPMainActivity = GetTopAPPMainActivity.split(" ")[9];
64 | return GetTopAPPMainActivity;
65 | }catch (Exception e){
66 | return "APP NO LAUNCHER";
67 | }
68 | }
69 |
70 | // 获取顶层应用程序包名 ;获取屏幕顶层应用进程名称 adb shell dumpsys activity activities|grep app=ProcessRecord
71 | public static void GetPIDPackageName(String packName){
72 | String adbshell = "adb "+ deviceInfo.getSelectDevice() +" shell dumpsys activity activities|grep :" + packName;
73 | adbshell = CmdPull(adbshell);
74 |
75 | // 获取应用程序包名
76 | String result = adbshell;
77 | String PackName = result.split(":")[1].split("/")[0];
78 |
79 | // 获取应用程序PID进程名
80 | adbshell = adbshell.split("\\s+")[2];
81 | String pidpackagename = adbshell.split("/")[1];
82 | pidpackagename = pidpackagename.substring(0,pidpackagename.length()-1);
83 | // 异常处理应用进程名错误时获取其包名
84 | try {
85 | pidpackagename = pidpackagename.substring(0,pidpackagename.indexOf("a")) + "_" + pidpackagename.substring(pidpackagename.indexOf("a"),pidpackagename.length());
86 | }catch (Exception e){
87 | pidpackagename=PackName;
88 | }
89 | TopAPPInfo.setPIDPackageName(pidpackagename);
90 |
91 | // 获取到应用程序PID进程名就立即使用执行获取 adb shell ps|grep
92 | GetPackNameResult();
93 | GetSystemResult();
94 |
95 | }
96 |
97 |
98 | // 执行获取进程PID的信息 > adb shell ps|grep u0_a1005
99 | public static String GetAPPPID(String CompatibleOS,String PackName){
100 | GetAPPPID = "adb "+ deviceInfo.getSelectDevice() +" shell ps " + CompatibleOS + " |grep "+ PackName;
101 | return GetAPPPID;
102 | }
103 |
104 | // 获取APP的进程PID
105 | public static String GetTopAPPBit(String PackName){
106 | String PackNameResult = CmdPull(GetAPPPID(CompatibleOS,PackName));
107 | if( PackNameResult.length() < 10 ){
108 | CompatibleOS = "-A";
109 | SetPID(CompatibleOS,PackName);
110 | return TopAPPInfo.getPackBit();
111 | }else {
112 | SetPID(CompatibleOS,PackName);
113 | return TopAPPInfo.getPackBit();
114 | }
115 | }
116 |
117 | // 获取adb shell ps|grep u0_a1005过滤信息
118 | public static void GetPackNameResult(){
119 | String PackNameResult = CmdPull(GetAPPPID(CompatibleOS,TopAPPInfo.getPIDPackageName()));
120 | TopAPPInfo.setPackNameResult(PackNameResult);
121 | }
122 |
123 |
124 | // 获取adb shell ps|grep zygote64过滤信息
125 | public static void GetSystemResult(){
126 | String SystemResult = CmdPull(GetSystemPID(CompatibleOS));
127 | TopAPPInfo.setSystemResult(SystemResult);
128 | }
129 |
130 | // 根据获取到的 系统进程PID 与 应用程序PID 判断运行程序的位数
131 | public static void SetPID(String CompatibleOS,String PackName ){
132 |
133 | // 获取APP PackName PID进程ID
134 | GetAPPPID = GetAPPPID(CompatibleOS,PackName);
135 |
136 | String PackNameResult = TopAPPInfo.getPackNameResult();
137 | GetAPPPID = PackNameResult.split("\\s+")[2];
138 | TopAPPInfo.setPIDPackName(GetAPPPID);
139 |
140 | // 获取系统 64位zygote PID进程ID
141 | SystemResult = TopAPPInfo.getSystemResult();
142 |
143 | try {
144 | // 解决获取多个64位zygote的情况的bug,导致判断程序位数不准确
145 | String[] testss = SystemResult.split("\n");
146 | if(testss.length == 1){
147 | SystemResult = SystemResult.split("\\s+")[1];
148 | TopAPPInfo.setPIDzygote(SystemResult);
149 | }else if( testss.length ==2 ){
150 | SystemResult = testss[0].split("\\s+")[1];
151 | TopAPPInfo.setPIDzygote(SystemResult);
152 |
153 | SystemResult = testss[1].split("\\s+")[1];
154 | TopAPPInfo.setPIDzygotetwo(SystemResult);
155 | }
156 | }catch (Exception e){
157 | TopAPPInfo.setPIDzygote("NaN");
158 | }
159 |
160 | // 通过获取程序PID与系统的64位PID对比判断是否是 64位 或 32位程序
161 |
162 | // 未做为空判断
163 | if( TopAPPInfo.getPIDzygote().equals("") && TopAPPInfo.getPIDPackName().equals("") ){
164 | TopAPPInfo.setPackBit("32位 程序");
165 | }
166 | else if( TopAPPInfo.getPIDPackName().equals(TopAPPInfo.getPIDzygote()) ){
167 | TopAPPInfo.setPackBit("64位 程序");
168 | }else if( TopAPPInfo.getPIDPackName().equals(TopAPPInfo.getPIDzygotetwo()) ){
169 | TopAPPInfo.setPackBit("64位 程序");
170 | } else {
171 | TopAPPInfo.setPackBit("32位 程序");
172 | }
173 |
174 | }
175 |
176 |
177 |
178 | // 解决三星S9上获取多个64位进程pid的情况的bug
179 | public static String GetSystemPID(String CompatibleOS){
180 | GetSystemPID = "adb "+ deviceInfo.getSelectDevice() +" shell ps " + CompatibleOS + " |grep zygote64";
181 | return GetSystemPID;
182 | }
183 |
184 | public static String GetPmPath(String PackName){
185 | GetPmPath = "adb "+ deviceInfo.getSelectDevice() +" shell pm path " + PackName;
186 | return GetPmPath;
187 | }
188 |
189 | // 执行备份文件命令
190 | public static String GetPullFile(String PackPath, String PackName){
191 | GetPullFile = "adb "+ deviceInfo.getSelectDevice() +" pull " + PackPath + " "+ PackName + ".apk";
192 | GetPullFile = CmdPull(GetPullFile);
193 | // 判断备份是否成功
194 | if (GetPullFile.indexOf("error")!=-1){
195 | // -1 不包含,其他为包含
196 | // 包含错误信息时
197 | TopAPPInfo.setPullStatus(GetPullFile);
198 |
199 | }else{
200 | // 不包含错误信息时
201 | TopAPPInfo.setPullStatus("Success");
202 | }
203 | return GetPullFile;
204 | }
205 |
206 | // 执行关闭APP命令
207 | public static String CloseAPP(String PackName){
208 | CloseAPP = "adb "+ deviceInfo.getSelectDevice() +" shell am force-stop " + PackName;
209 | CloseAPP = CmdPull(CloseAPP);
210 | return CloseAPP;
211 | }
212 |
213 | // 执行清理APP缓存命令
214 | public static String CleanAPP(String PackName){
215 | CleanAPP = "adb "+ deviceInfo.getSelectDevice() +" shell pm clear " + PackName;
216 | CleanAPP = CmdPull(CleanAPP);
217 | // 判断清理是否成功
218 | if (CleanAPP.indexOf("Error")!=-1){
219 | // -1 不包含,其他为包含
220 | // 包含错误信息时
221 | TopAPPInfo.setClearStatus(CleanAPP);
222 |
223 | }else{
224 | // 不包含错误信息时
225 | TopAPPInfo.setClearStatus("Success");
226 | }
227 | return CleanAPP;
228 | }
229 |
230 | // 执行启动APP命令
231 | public static String StartAPPLaunchableActivity(String LaunchableActivity){
232 | StartAPP = "adb "+ deviceInfo.getSelectDevice() +" shell am start -n" + LaunchableActivity;
233 | StartAPP = CmdPull(StartAPP);
234 | return StartAPP;
235 | }
236 |
237 | // 执行卸载APP命令
238 | public static String UninstallAPP(String PackName){
239 | UninstallAPP = "adb "+ deviceInfo.getSelectDevice() +" uninstall " + PackName;
240 | UninstallAPP = CmdPull(UninstallAPP);
241 | // 判断卸载是否成功
242 | if (UninstallAPP.indexOf("Failure")!=-1 || UninstallAPP.indexOf("Exception") != -1 || UninstallAPP.indexOf("error") != -1 ){
243 | // -1 不包含,其他为包含
244 | // 包含错误信息时
245 | TopAPPInfo.setUninstallStatus(UninstallAPP);
246 | }else{
247 | // 不包含错误信息时
248 | TopAPPInfo.setUninstallStatus("Success");
249 | }
250 | return UninstallAPP;
251 | }
252 |
253 | // 获取 app versionName, versionCode, minSdk, targetSdk
254 | public static String GetAppInfo(String PackName){
255 | GetAppInfo = "adb "+ deviceInfo.getSelectDevice() +" shell dumpsys package " + PackName + "|grep version";
256 | GetAppInfo = CmdPull(GetAppInfo);
257 | return GetAppInfo;
258 | }
259 |
260 | // 正则匹配 AppInfo,
261 | public static String GetAppInfo(String GetAppInfo,String re) {
262 | Pattern FindString = Pattern.compile(re);
263 | Matcher FindStringInfo = FindString.matcher(GetAppInfo);
264 | String FindSring;
265 | if ( FindStringInfo.find( )) {
266 | FindSring = FindStringInfo.group(1) ;
267 | }else {
268 | FindSring = "null" ;
269 | }
270 | return FindSring;
271 | }
272 |
273 | // adb命令执行后,设置实例对象的值
274 | public static void SetAppInfo(String PackName){
275 | GetAppInfo = GetAppInfo(PackName);
276 | TopAPPInfo.setVersionCode(GetAppInfo(GetAppInfo,"versionCode=(\\d+)"));
277 | TopAPPInfo.setVersionName(GetAppInfo(GetAppInfo,"versionName=(\\d+(\\.\\d+)*)"));
278 | TopAPPInfo.setMinSdk(GetAppInfo(GetAppInfo,"minSdk=(\\d+)"));
279 | TopAPPInfo.setTargetSdk(GetAppInfo(GetAppInfo,"targetSdk=(\\d+)"));
280 | }
281 |
282 | // 获取屏幕焦点应用信息
283 | public static String GetmCurrentFocus(){
284 | // mCurrentFocus = CmdPull("adb "+ deviceInfo.getSelectDevice() +" shell dumpsys window | grep mCurrentFocus");
285 | mCurrentFocus = CmdPull("adb "+ deviceInfo.getSelectDevice() +" shell dumpsys window | grep mFocusedWindow=Window");
286 | String FindSring = null;
287 | try {
288 | FindSring = mCurrentFocus.split("\\s+")[3];
289 | if(FindSring.length() < 3){
290 | FindSring = mCurrentFocus.split("\\s+")[4];
291 | }
292 | FindSring = FindSring.substring(0,FindSring.length()-1);
293 | }catch (Exception e){
294 | FindSring = null;
295 | }
296 |
297 | // 正则表达式部分代码
298 | // Pattern FindString = Pattern.compile("u.*? (.*?)}");
299 | // Matcher FindStringInfo = FindString.matcher(mCurrentFocus);
300 | // String FindSring;
301 | // if ( FindStringInfo.find( )) {
302 | // FindSring = FindStringInfo.group(1) ;
303 | // }else {
304 | // FindSring = "null" ;
305 | // }
306 | TopAPPInfo.setmCurrentFocus(FindSring);
307 | System.out.println("\nAndroid Screen mCurrentFocus: " + FindSring);
308 | System.out.println("Android Devices TopActivity: " + TopAPPInfo.getPackTopActivity());
309 | return FindSring;
310 | }
311 |
312 | // 获取设备状态的线程组
313 | public static void GetDevicesStatus() throws InterruptedException {
314 | GetAdbDevicesThread t10 = new GetAdbDevicesThread();
315 | Thread thread10 = new Thread(t10);
316 | thread10.start();
317 | thread10.join();
318 | }
319 |
320 | // 获取信息主线程组
321 | public static void DoGetAPP() throws InterruptedException {
322 |
323 | GetPackNameThread t1 = new GetPackNameThread();
324 | GetTopAPPMainActivityThread t2 = new GetTopAPPMainActivityThread();
325 | GetTopAPPBitThread t3 = new GetTopAPPBitThread();
326 | GetPmPathThread t4 = new GetPmPathThread();
327 | GetAppInfoThread t5 = new GetAppInfoThread();
328 | GetmCurrentFocusThread t6 = new GetmCurrentFocusThread();
329 | GetPIDPackageNameThread t7 = new GetPIDPackageNameThread();
330 |
331 | Thread thread1 = new Thread(t1);
332 | Thread thread2 = new Thread(t2);
333 | Thread thread3 = new Thread(t3);
334 | Thread thread4 = new Thread(t4);
335 | Thread thread5 = new Thread(t5);
336 | Thread thread6 = new Thread(t6);
337 | Thread thread7 = new Thread(t7);
338 |
339 | thread1.start();
340 | thread1.join();
341 |
342 | thread7.start();
343 | thread6.start();
344 | thread7.join();
345 | thread6.join();
346 | System.out.println( "> adb shell ps|grep zygote64 && adb shell ps|grep " + TopAPPInfo.getPIDPackageName() + "\n" + TopAPPInfo.getSystemResult() + TopAPPInfo.getPackNameResult());
347 |
348 | thread2.start();
349 | thread3.start();
350 | thread4.start();
351 | thread5.start();
352 |
353 | thread2.join();
354 | thread3.join();
355 | thread4.join();
356 | thread5.join();
357 |
358 | }
359 | }
360 |
361 |
362 | // 获取手机 第一界面APP包名
363 | class GetPackNameThread implements Runnable {
364 | private static String GetTopAPP;
365 | public void run() {
366 | // 获取顶层APP程序包名和Activity
367 | GetTopAPP = "adb "+ deviceInfo.getSelectDevice() +" shell dumpsys activity activities |grep -i hist |grep visible=true -1";
368 | GetTopAPP = DevicesTopAPP.CmdPull(GetTopAPP).split(" ")[11]; // 此处有坑,不支持 32位 手机 head -1
369 | TopAPPInfo.setPackTopActivity(GetTopAPP);
370 | TopAPPInfo.setPackName(GetTopAPP.split("/")[0]);
371 | }
372 | }
373 |
374 | // 获取手机 第一界面PS应用进程名
375 | class GetPIDPackageNameThread implements Runnable {
376 | public void run() {
377 | DevicesTopAPP.GetPIDPackageName(TopAPPInfo.getPackName());
378 | }
379 | }
380 |
381 |
382 | // 获取 mCurrentFocus
383 | class GetmCurrentFocusThread implements Runnable{
384 | public void run(){
385 | DevicesTopAPP.GetmCurrentFocus();
386 | }
387 | }
388 |
389 | // 获取手机 第一界面APP包名
390 | class GetTopAPPMainActivityThread implements Runnable {
391 | private static String GetTopAPPMainActivity;
392 | private static String PackName;
393 | public void run() {
394 | // 获取顶层APP的第一启动启动界面Activity
395 | PackName = TopAPPInfo.getPackName(); // 获取程序包名
396 | GetTopAPPMainActivity = DevicesTopAPP.CmdPull(DevicesTopAPP.GetTopAPPMainActivity(PackName));
397 | TopAPPInfo.setPackMainActivity(DevicesTopAPP.SetTopAPPMainActivity(GetTopAPPMainActivity)); // 异常处理部分程序无该界面
398 | }
399 | }
400 |
401 | // 获取顶层APP是否是 32位 或者 64位应用
402 | class GetTopAPPBitThread implements Runnable {
403 | private static String GetTopAPPBit;
404 | private static String PackName;
405 | public void run() {
406 | // 获取顶层APP的第一启动启动界面Activity
407 | PackName = TopAPPInfo.getPIDPackageName(); // 获取程序包名
408 | GetTopAPPBit = DevicesTopAPP.GetTopAPPBit(PackName);
409 | }
410 | }
411 |
412 | // 备份apk,存储apk路径
413 | class GetPmPathThread implements Runnable {
414 | private static String PackName;
415 | private static String GetPmPath;
416 | public void run() {
417 | // 备份apk,存储apk路径
418 | PackName = TopAPPInfo.getPackName();
419 | GetPmPath = DevicesTopAPP.CmdPull(DevicesTopAPP.GetPmPath(PackName));
420 | String[] GetPmPaths = GetPmPath.split("\n");
421 | TopAPPInfo.setPackPath(GetPmPaths[0].split(":")[1]);
422 | }
423 | }
424 |
425 | // 获取APP versionName, versionCode, minSdk, targetSdk
426 | class GetAppInfoThread implements Runnable {
427 | private static String PackName;
428 | public void run() {
429 | PackName = TopAPPInfo.getPackName();
430 | DevicesTopAPP.SetAppInfo(PackName);
431 | }
432 | }
433 |
434 | // 获取手机状态
435 | class GetAdbDevicesThread implements Runnable {
436 | private static String[] GetConsole;
437 | private static String AdbDevices;
438 | public void run() {
439 | AdbDevices = DevicesTopAPP.CmdPull(DevicesTopAPP.GetAdbDevices);
440 | // 遍历获取设备信息
441 | getDevices.getDevicesInfo(AdbDevices);
442 | deviceInfo.setDevicesRes(AdbDevices);
443 | GetConsole = AdbDevices.split("\n");
444 | int GetConsoleLenth = GetConsole.length;
445 | if( GetConsoleLenth == 2 && GetConsole[1].indexOf("device") != -1) {
446 | TopAPPInfo.setAdbDevices("Success");
447 | }else if (GetConsoleLenth >= 3){
448 | TopAPPInfo.setAdbDevices("请检查电脑是否连接多台设备?");
449 | }else{
450 | TopAPPInfo.setAdbDevices("请检查设备是否正常连接!");
451 | }
452 | }
453 |
454 | }
--------------------------------------------------------------------------------
/src/main/java/MainUITest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Benjamin on 2018/12/4.
3 | */
4 |
5 | import javax.swing.*;
6 | import java.awt.*;
7 | import java.awt.event.ActionEvent;
8 | import java.awt.event.ActionListener;
9 |
10 |
11 | public class MainUITest extends JFrame implements ActionListener {
12 |
13 | // 定义组件
14 | JButton EnterBtn,GetFile,CloseBtn,CleanAndStartBtn; // 定义确认按钮
15 | JMenuItem MenuScreenshot,MenuUninstall,MenuSelect,MenuAbout;
16 | JLabel PackBit,versionCode, versionName, minSdk, targetSdk;
17 | JTextField PackName,PackPath,Launchable_Activity,TopActivity;
18 |
19 |
20 | public static void main(String[] args) {
21 | deviceInfo.setSelectDevice(" ");
22 | MainUITest mainUI=new MainUITest();
23 | }
24 |
25 | public MainUITest() {
26 |
27 | // 创建组件并设置监听
28 | EnterBtn = new JButton("获取APK信息");
29 | GetFile = new JButton("备份APK");
30 | CloseBtn = new JButton("关闭当前程序");
31 | CleanAndStartBtn = new JButton("清除数据并启动");
32 |
33 | EnterBtn.addActionListener(this);
34 | GetFile.addActionListener(this);
35 | CloseBtn.addActionListener(this);
36 | CleanAndStartBtn.addActionListener(this);
37 |
38 | //初始化一个菜单栏
39 | MenuScreenshot = new JMenuItem("截图");
40 | MenuUninstall = new JMenuItem("卸载");
41 | // MenuSelect = new JMenuItem("操作");
42 | MenuAbout = new JMenuItem("About");
43 |
44 | JMenuBar menuScreenshot = new JMenuBar();
45 | JMenuBar menuUninstall = new JMenuBar();
46 | // JMenuBar menuSelect = new JMenuBar();
47 | JMenuBar menuAbout = new JMenuBar();
48 |
49 | menuAbout.add(MenuScreenshot);
50 | menuAbout.add(MenuUninstall);
51 | // menuAbout.add(MenuSelect);
52 | menuAbout.add(MenuAbout);
53 | myEvent(); // 加载菜单栏监听事件处理
54 |
55 | // 信息栏选项
56 | getContentPane().add(new JLabel("PackName:", SwingConstants.CENTER ));
57 | PackName = new JTextField("PackName",10);
58 | getContentPane().add(PackName);
59 |
60 | getContentPane().add(new JLabel("PackBit:", SwingConstants.CENTER ));
61 | PackBit = new JLabel("PackBit");
62 | getContentPane().add(PackBit);
63 |
64 | getContentPane().add(new JLabel("Launchable_Activity:", SwingConstants.CENTER ));
65 | Launchable_Activity = new JTextField("Launchable_Activity",10);
66 | getContentPane().add(Launchable_Activity);
67 |
68 | getContentPane().add(new JLabel("TopActivity:", SwingConstants.CENTER ));
69 | TopActivity = new JTextField("TopActivity",10);
70 | getContentPane().add(TopActivity);
71 |
72 | getContentPane().add(new JLabel("PackPath:", SwingConstants.CENTER ));
73 | PackPath = new JTextField("PackPath",10);
74 | getContentPane().add(PackPath);
75 |
76 | // 新增 app versionCode, versionName, minSdk, targetSdk
77 | getContentPane().add(new JLabel("versionName:", SwingConstants.CENTER ));
78 | versionName = new JLabel("versionName");
79 | getContentPane().add(versionName);
80 |
81 | getContentPane().add(new JLabel("versionCode:", SwingConstants.CENTER ));
82 | versionCode = new JLabel("versionCode");
83 | getContentPane().add(versionCode);
84 |
85 | getContentPane().add(new JLabel("minSdk:", SwingConstants.CENTER ));
86 | minSdk = new JLabel("minSdk");
87 | getContentPane().add(minSdk);
88 |
89 | getContentPane().add(new JLabel("targetSdk:", SwingConstants.CENTER ));
90 | targetSdk = new JLabel("targetSdk");
91 | getContentPane().add(targetSdk);
92 |
93 | // 添加按钮
94 | getContentPane().add(EnterBtn);
95 | getContentPane().add(GetFile);
96 | getContentPane().add(CloseBtn);
97 | getContentPane().add(CleanAndStartBtn);
98 |
99 | this.setJMenuBar(menuScreenshot); //设置菜单栏 截图
100 | this.setJMenuBar(menuUninstall);
101 | // this.setJMenuBar(menuSelect);
102 | this.setJMenuBar(menuAbout); //设置菜单栏 关于
103 |
104 | this.setLayout(new GridLayout(0,2)); //选择GridLayout布局管理器
105 | this.setTitle("APP-TestTools");
106 | this.setSize(400,320);
107 | this.setLocation(0, 0);
108 | this.setLocationRelativeTo(null);//窗体居中显示
109 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置当关闭窗口时,保证JVM也退出
110 | this.setVisible(true);
111 | this.setResizable(true);
112 |
113 | }
114 |
115 | // 菜单栏监听
116 | private void myEvent()
117 | {
118 | // 截图
119 | MenuScreenshot.addActionListener(new ActionListener() {
120 | public void actionPerformed(ActionEvent e) {
121 | ScreenshotUtil.ScreencapShot();
122 | // 截图判断是否成功
123 | if( TopAPPInfo.getScreencapStatus().equals("Success") == false ){
124 | JOptionPane.showMessageDialog(null, "Screenshot Error!\nPlease Check You Phone USB Connect." , "Faild",JOptionPane.WARNING_MESSAGE);
125 | return;
126 | }
127 | JOptionPane.showMessageDialog(null, "Please Check Jar Current Path File: \nFileName: screenshot" + TopAPPInfo.getScreencapTime()+".png" , "Success",JOptionPane.INFORMATION_MESSAGE);
128 | return;
129 | }
130 | });
131 |
132 | // 卸载
133 | MenuUninstall.addActionListener(new ActionListener() {
134 | public void actionPerformed(ActionEvent e) {
135 |
136 | // 获取设备信息;fix uninstall APP bug
137 | if( GetDevicesStatus().equals("0")){ return; }
138 |
139 | if(TopAPPInfo.getPackPath() == null | TopAPPInfo.getPackPath() == ""){
140 | JOptionPane.showMessageDialog(null,"请先获取APK信息!","提示消息",JOptionPane.WARNING_MESSAGE);
141 | return;
142 | }else {
143 | // 执行卸载APP
144 | DevicesTopAPP.UninstallAPP(TopAPPInfo.getPackName());
145 | if( TopAPPInfo.getUninstallStatus().equals("Success") == false ){
146 | JOptionPane.showMessageDialog(null, TopAPPInfo.getUninstallStatus() , "Faild",JOptionPane.WARNING_MESSAGE);
147 | return;
148 | }else {
149 | JOptionPane.showMessageDialog(null, "Uninstall Sucess PackName: " + TopAPPInfo.getPackName() , "Sucess",JOptionPane.INFORMATION_MESSAGE);
150 | return;
151 | }
152 | }
153 | }
154 | });
155 |
156 | // About
157 | MenuAbout.addActionListener(new ActionListener() {
158 | public void actionPerformed(ActionEvent e) {
159 | // 隐藏的设备选择功能
160 | Object[] options ={ "好啊"};
161 | int m = JOptionPane.showOptionDialog(null, "Author: Benjamin\nWeChat: WeChat_Benjamin\nEmail: Benjamin_v@qq.com", "AboutInfo",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
162 | // 判断是否连接多台设备,并选择设备
163 | if( m == 0 ) {
164 | try {
165 | if(TopAPPInfo.getAdbDevices().equals("请检查电脑是否连接多台设备?")){
166 | String[] deviceslist =deviceInfo.getDeviceslist();
167 | String s = (String) JOptionPane.showInputDialog(null,"请选择指定设备:\n", "设备列表", JOptionPane.PLAIN_MESSAGE, null, deviceslist, "Benjamin");
168 | // 选择设备
169 | if(s != null){
170 | deviceInfo.setSelectDevice(" -s " + deviceInfo.getDevicesInfo().get(s) + " ");
171 | deviceInfo.setDevicesID((String) deviceInfo.getDevicesInfo().get(s));
172 | }
173 | }
174 | }catch (Exception e1){
175 | }
176 | }
177 | return;
178 | }
179 | });
180 |
181 | }
182 |
183 | // 界面按钮监听
184 | @Override
185 | public void actionPerformed(ActionEvent e) {
186 |
187 | // 获取设备信息
188 | if( GetDevicesStatus().equals("0")){ return; }
189 |
190 | if(e.getActionCommand()=="获取APK信息") {
191 | try {
192 | DevicesTopAPP.DoGetAPP();
193 | } catch (Exception e1) {
194 | e1.printStackTrace();
195 | }
196 | PackName.setText(TopAPPInfo.getPackName());
197 | PackBit.setText(TopAPPInfo.getPackBit());
198 | Launchable_Activity.setText(TopAPPInfo.getPackMainActivity());
199 | TopActivity.setText(TopAPPInfo.getPackTopActivity());
200 | PackPath.setText(TopAPPInfo.getPackPath());
201 |
202 | versionCode.setText(TopAPPInfo.getVersionCode());
203 | versionName.setText(TopAPPInfo.getVersionName());
204 | minSdk.setText(TopAPPInfo.getMinSdk());
205 | targetSdk.setText(TopAPPInfo.getTargetSdk());
206 | return;
207 |
208 | }
209 |
210 |
211 | // 判断获取到app信息
212 | if(TopAPPInfo.getPackPath() == null | TopAPPInfo.getPackPath() == ""){
213 | JOptionPane.showMessageDialog(null,"请先获取APK信息!","提示消息",JOptionPane.WARNING_MESSAGE);
214 | return;
215 | }
216 |
217 | if(e.getActionCommand()=="备份APK"){
218 | //执行pull备份到本地
219 | DevicesTopAPP.GetPullFile(TopAPPInfo.getPackPath(),TopAPPInfo.getPackName());
220 | if( TopAPPInfo.getPullStatus().equals("Success") == false ){
221 | JOptionPane.showMessageDialog(null,TopAPPInfo.getPullStatus(),"Error!",JOptionPane.WARNING_MESSAGE);
222 | return;
223 | }
224 | JOptionPane.showMessageDialog(null,"备份成功,请查看本地目录!","提示消息",JOptionPane.WARNING_MESSAGE);
225 | return;
226 | }else if(e.getActionCommand()=="清除数据并启动"){
227 | //执行cmd命令, 清除、启动
228 | DevicesTopAPP.CleanAPP(TopAPPInfo.getPackName());
229 | if( TopAPPInfo.getClearStatus().equals("Success") == false ) {
230 | JOptionPane.showMessageDialog(null,TopAPPInfo.getClearStatus(),"Error!",JOptionPane.WARNING_MESSAGE);
231 | return;
232 | }
233 | DevicesTopAPP.StartAPPLaunchableActivity(TopAPPInfo.getPackMainActivity());
234 | return;
235 | }else if(e.getActionCommand()=="关闭当前程序"){
236 | //执行cmd命令,关闭当前程序
237 | DevicesTopAPP.CloseAPP(TopAPPInfo.getPackName());
238 | return;
239 | }
240 |
241 | }
242 |
243 | // 获取设备状态信息
244 | public static String GetDevicesStatus() {
245 | // 获取设备信息
246 | try {
247 | DevicesTopAPP.GetDevicesStatus();
248 | } catch (InterruptedException e1) {
249 | e1.printStackTrace();
250 | }
251 |
252 |
253 | if( !TopAPPInfo.getAdbDevices().equals("Success") ) {
254 |
255 | if(TopAPPInfo.getAdbDevices().equals("请检查电脑是否连接多台设备?")) {
256 | if (deviceInfo.getDevicesID() != null && deviceInfo.getDevicesID() != "") {
257 | boolean status = deviceInfo.getDevicesRes().contains(deviceInfo.getDevicesID());
258 | if (status) {
259 | deviceInfo.setSelectDevice(" -s " + deviceInfo.getDevicesID() + " ");
260 | return "1";
261 | }
262 | }
263 | }
264 |
265 | JOptionPane.showMessageDialog(null,TopAPPInfo.getAdbDevices(),"提示消息",JOptionPane.WARNING_MESSAGE);
266 | return "0";
267 | }
268 |
269 | deviceInfo.setSelectDevice("");
270 | return "1";
271 | }
272 |
273 |
274 |
275 |
276 | }
277 |
--------------------------------------------------------------------------------
/src/main/java/ScreenshotUtil.java:
--------------------------------------------------------------------------------
1 | import java.text.SimpleDateFormat;
2 | import java.util.Date;
3 |
4 | /**
5 | * Created by Benjamin on 2019/3/6.
6 | */
7 |
8 | public class ScreenshotUtil {
9 |
10 | public static String RmImage = "adb shell rm -rf /sdcard/screenshot.png";
11 | public static String Screencap = "adb shell screencap -p /sdcard/screenshot.png";
12 | public static String PullImage = "adb pull /sdcard/screenshot.png /Users/xielianshi/Desktop";
13 |
14 | // 获取当前时间
15 | public static String GetNowTime(){
16 | //设置日期格式
17 | SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
18 | return df.format(new Date());
19 | }
20 |
21 | // 执行截图命令
22 | public static String Screencap(){
23 | TopAPPInfo.setScreencapTime(ScreenshotUtil.GetNowTime());
24 | Screencap = "adb "+ deviceInfo.getSelectDevice() +" shell screencap -p /sdcard/screenshot" + TopAPPInfo.getScreencapTime() + ".png";
25 | Screencap = DevicesTopAPP.CmdPull(Screencap);
26 | // 判断截图是否成功
27 | if (Screencap.indexOf("error")!=-1){
28 | // -1 不包含,其他为包含
29 | // 包含错误信息时
30 | TopAPPInfo.setScreencapStatus("Faild");
31 |
32 | }else{
33 | // 不包含错误信息时
34 | TopAPPInfo.setScreencapStatus("Success");
35 | }
36 | return Screencap;
37 | }
38 |
39 | // 执行pull同步图片命令
40 | public static String PullImage(){
41 | PullImage = "adb "+ deviceInfo.getSelectDevice() +" pull /sdcard/screenshot" + TopAPPInfo.getScreencapTime() + ".png";
42 | PullImage = DevicesTopAPP.CmdPull(PullImage);
43 | return PullImage;
44 | }
45 |
46 | // 执行删除命令
47 | public static String RmImage(){
48 | RmImage = "adb "+ deviceInfo.getSelectDevice() +" shell rm -rf /sdcard/screenshot" + TopAPPInfo.getScreencapTime() + ".png";
49 | RmImage = DevicesTopAPP.CmdPull(RmImage);
50 | return RmImage;
51 | }
52 |
53 | // 操作方法组合
54 | public static String ScreencapShot(){
55 | Screencap();
56 | PullImage();
57 | RmImage();
58 | return "Success";
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/StreamCaptureThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Benjamin on 2018/12/3.
3 | */
4 |
5 | import java.io.BufferedReader;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.InputStreamReader;
9 |
10 | public class StreamCaptureThread implements Runnable {
11 |
12 | InputStream stream;
13 | StringBuilder output;
14 |
15 | public StreamCaptureThread(InputStream stream) {
16 | this.stream = stream;
17 | this.output = new StringBuilder();
18 | }
19 |
20 | public void run() {
21 | try {
22 | try {
23 | BufferedReader br = new BufferedReader(
24 | new InputStreamReader(this.stream));
25 | String line = br.readLine();
26 | while (line != null) {
27 | if (line.trim().length() > 0) {
28 | output.append(line).append("\n");
29 | }
30 | line = br.readLine();
31 | }
32 | } catch (IOException e) {
33 | e.printStackTrace();
34 | } finally {
35 | if (stream != null) {
36 | stream.close();
37 | }
38 | }
39 | } catch (IOException ex) {
40 | ex.printStackTrace(System.err);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/TopAPPInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Benjamin on 2018/12/4.
3 | */
4 |
5 |
6 | public class TopAPPInfo {
7 |
8 | public static String AdbDevices;
9 | public static String PackName;
10 | public static String PackPath;
11 | public static String mCurrentFocus;
12 | public static String PIDzygote;
13 | public static String PIDzygotetwo;
14 | public static String PIDPackName;
15 | public static String PIDPackageName;
16 | public static String PackMainActivity;
17 | public static String PackTopActivity;
18 | public static String PackBit;
19 | public static String versionCode;
20 | public static String versionName;
21 | public static String minSdk;
22 | public static String targetSdk;
23 |
24 | public static String ScreencapTime;
25 | public static String ScreencapStatus;
26 |
27 | public static String ClearStatus;
28 | public static String UninstallStatus;
29 | public static String PullStatus;
30 |
31 | public static String PackNameResult;
32 | public static String SystemResult;
33 |
34 | public static String getPIDzygotetwo() {
35 | return PIDzygotetwo;
36 | }
37 |
38 | public static void setPIDzygotetwo(String PIDzygotetwo) {
39 | TopAPPInfo.PIDzygotetwo = PIDzygotetwo;
40 | }
41 |
42 | public static String getPackBit() {
43 | return PackBit;
44 | }
45 |
46 | public static void setPackBit(String packBit) {
47 | PackBit = packBit;
48 | }
49 |
50 | public static String getAdbDevices() {
51 | return AdbDevices;
52 | }
53 |
54 | public static void setAdbDevices(String adbDevices) {
55 | AdbDevices = adbDevices;
56 | }
57 |
58 | public static String getPackName() {
59 | return PackName;
60 | }
61 |
62 | public static void setPackName(String packName) {
63 | PackName = packName;
64 | }
65 |
66 | public static String getPackPath() {
67 | return PackPath;
68 | }
69 |
70 | public static void setPackPath(String packPath) {
71 | PackPath = packPath;
72 | }
73 |
74 | public static String getmCurrentFocus() {
75 | return mCurrentFocus;
76 | }
77 |
78 | public static void setmCurrentFocus(String mCurrentFocus) {
79 | TopAPPInfo.mCurrentFocus = mCurrentFocus;
80 | }
81 |
82 | public static String getPIDzygote() {
83 | return PIDzygote;
84 | }
85 |
86 | public static void setPIDzygote(String PIDzygote) {
87 | TopAPPInfo.PIDzygote = PIDzygote;
88 | }
89 |
90 | public static String getPIDPackName() {
91 | return PIDPackName;
92 | }
93 |
94 | public static void setPIDPackName(String PIDPackName) {
95 | TopAPPInfo.PIDPackName = PIDPackName;
96 | }
97 |
98 | public static String getPIDPackageName() {
99 | return PIDPackageName;
100 | }
101 |
102 | public static void setPIDPackageName(String PIDPackageName) {
103 | TopAPPInfo.PIDPackageName = PIDPackageName;
104 | }
105 |
106 | public static String getPackMainActivity() {
107 | return PackMainActivity;
108 | }
109 |
110 | public static void setPackMainActivity(String packMainActivity) {
111 | PackMainActivity = packMainActivity;
112 | }
113 |
114 | public static String getPackTopActivity() {
115 | return PackTopActivity;
116 | }
117 |
118 | public static void setPackTopActivity(String packTopActivity) {
119 | PackTopActivity = packTopActivity;
120 | }
121 |
122 | public static String getVersionCode() {
123 | return versionCode;
124 | }
125 |
126 | public static void setVersionCode(String versionCode) {
127 | TopAPPInfo.versionCode = versionCode;
128 | }
129 |
130 | public static String getVersionName() {
131 | return versionName;
132 | }
133 |
134 | public static void setVersionName(String versionName) {
135 | TopAPPInfo.versionName = versionName;
136 | }
137 |
138 | public static String getMinSdk() {
139 | return minSdk;
140 | }
141 |
142 | public static void setMinSdk(String minSdk) {
143 | TopAPPInfo.minSdk = minSdk;
144 | }
145 |
146 | public static String getTargetSdk() {
147 | return targetSdk;
148 | }
149 |
150 | public static void setTargetSdk(String targetSdk) {
151 | TopAPPInfo.targetSdk = targetSdk;
152 | }
153 |
154 | public static String getScreencapTime() {
155 | return ScreencapTime;
156 | }
157 |
158 | public static void setScreencapTime(String screencapTime) {
159 | ScreencapTime = screencapTime;
160 | }
161 |
162 | public static String getScreencapStatus() {
163 | return ScreencapStatus;
164 | }
165 |
166 | public static void setScreencapStatus(String screencapStatus) {
167 | ScreencapStatus = screencapStatus;
168 | }
169 |
170 | public static String getClearStatus() {
171 | return ClearStatus;
172 | }
173 |
174 | public static void setClearStatus(String clearStatus) {
175 | ClearStatus = clearStatus;
176 | }
177 |
178 | public static String getUninstallStatus() {
179 | return UninstallStatus;
180 | }
181 |
182 | public static void setUninstallStatus(String uninstallStatus) {
183 | UninstallStatus = uninstallStatus;
184 | }
185 |
186 | public static String getPullStatus() {
187 | return PullStatus;
188 | }
189 |
190 | public static void setPullStatus(String pullStatus) {
191 | PullStatus = pullStatus;
192 | }
193 |
194 | public static String getPackNameResult() {
195 | return PackNameResult;
196 | }
197 |
198 | public static void setPackNameResult(String packNameResult) {
199 | PackNameResult = packNameResult;
200 | }
201 |
202 | public static String getSystemResult() {
203 | return SystemResult;
204 | }
205 |
206 | public static void setSystemResult(String systemResult) {
207 | SystemResult = systemResult;
208 | }
209 | }
210 |
--------------------------------------------------------------------------------
/src/main/java/deviceInfo.java:
--------------------------------------------------------------------------------
1 | import java.util.Map;
2 |
3 | /**
4 | * Created by Benjamin on 2019/8/26.
5 | */
6 | public class deviceInfo {
7 |
8 | public static String selectDevice;
9 | public static String devicesID;
10 | public static String devicesRes;
11 | public static String[] deviceslist;
12 | public static Map devicesInfo;
13 |
14 | public static String getSelectDevice() {
15 | return selectDevice;
16 | }
17 |
18 | public static void setSelectDevice(String selectDevice) {
19 | deviceInfo.selectDevice = selectDevice;
20 | }
21 |
22 |
23 | public static String[] getDeviceslist() {
24 | return deviceslist;
25 | }
26 |
27 | public static String getDevicesID() {
28 | return devicesID;
29 | }
30 |
31 | public static String getDevicesRes() {
32 | return devicesRes;
33 | }
34 |
35 | public static void setDevicesRes(String devicesRes) {
36 | deviceInfo.devicesRes = devicesRes;
37 | }
38 |
39 | public static void setDevicesID(String devicesID) {
40 | deviceInfo.devicesID = devicesID;
41 | }
42 |
43 | public static void setDeviceslist(String[] deviceslist) {
44 | deviceInfo.deviceslist = deviceslist;
45 | }
46 |
47 | public static Map getDevicesInfo() {
48 | return devicesInfo;
49 | }
50 |
51 | public static void setDevicesInfo(Map devicesInfo) {
52 | deviceInfo.devicesInfo = devicesInfo;
53 | }
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/src/main/java/getDevices.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.HashMap;
3 | import java.util.Map;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | /**
8 | * Created by Benjamin on 2019/8/26.
9 | */
10 | public class getDevices {
11 | private static String[] devicelist = new String[0];
12 |
13 | // 获取电脑连接的所有设备id的brand、model
14 | public static void getDevicesInfo(String getDevicesRes){
15 | getDevices(getDevicesRes);
16 | Map devicesInfo = new HashMap();
17 | for(String device: devicelist){
18 | getBrandModel(device,devicesInfo);
19 | }
20 |
21 | String[] devicelist = new String[0];
22 | for (Object device : devicesInfo.keySet()) {
23 | devicelist = insert(devicelist,device.toString());
24 | }
25 |
26 | deviceInfo.setDeviceslist(devicelist);
27 | deviceInfo.setDevicesInfo(devicesInfo);
28 | }
29 |
30 | // 获取adb devices设备列表中的设备id信息
31 | private static void getDevices(String getDevicesRes){
32 | String[] resList = getDevicesRes.split("\n");
33 | for( String resLine : resList ) {
34 | String[] devicesList = resLine.split("\\s+");
35 | if(devicesList.length == 2 && Arrays.asList(devicesList).contains("device")){
36 | for( String devices : devicesList ) {
37 | // 追加设备id
38 | if( !devices.equals("device") ){devicelist = insert(devicelist,devices);}
39 | }
40 | }
41 | }
42 | }
43 |
44 | // 获取设备的Brand、model
45 | private static void getBrandModel(String devicesMark,Map devices){
46 | String Brand,Model;
47 | String getBrandModel = "adb -s " + devicesMark + " shell getprop | grep product";
48 | String res = DevicesTopAPP.CmdPull(getBrandModel);
49 | // 正则表达式部分代码
50 | Matcher getBrand = Pattern.compile("\\[ro.product.brand\\]: \\[(.*?)\\]").matcher(res);
51 | if ( getBrand.find( )) {
52 | Brand = getBrand.group(1);
53 | }else {
54 | Brand = "null" ;
55 | }
56 | Matcher getModel = Pattern.compile("\\[ro.product.model\\]: \\[(.*?)\\]").matcher(res);
57 | if ( getModel.find( )) {
58 | Model = getModel.group(1);
59 | }else {
60 | Model = "null" ;
61 | }
62 | devices.put(Brand+" "+Model,devicesMark);
63 |
64 | }
65 |
66 | // 往字符串数组追加新数据
67 | private static String[] insert(String[] arr, String str) {
68 | int size = arr.length;
69 | String[] tmp = new String[size + 1];
70 | for (int i = 0; i < size; i++){
71 | tmp[i] = arr[i];
72 | }
73 | tmp[size] = str;
74 | return tmp;
75 | }
76 |
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/target/MobileDevicesTools-1.0-SNAPSHOT-jar-with-dependencies.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/target/MobileDevicesTools-1.0-SNAPSHOT-jar-with-dependencies.jar
--------------------------------------------------------------------------------
/target/MobileDevicesTools-1.0-SNAPSHOT.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/target/MobileDevicesTools-1.0-SNAPSHOT.jar
--------------------------------------------------------------------------------
/target/maven-archiver/pom.properties:
--------------------------------------------------------------------------------
1 | #Generated by Maven
2 | #Mon Aug 26 22:53:33 CST 2019
3 | version=1.0-SNAPSHOT
4 | groupId=MobileDevicesTools
5 | artifactId=MobileDevicesTools
6 |
--------------------------------------------------------------------------------
/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst:
--------------------------------------------------------------------------------
1 | DevicesTopAPP.class
2 | GetTopAPPMainActivityThread.class
3 | GetPIDPackageNameThread.class
4 | ScreenshotUtil.class
5 | MainUITest$1.class
6 | GetAppInfoThread.class
7 | GetPackNameThread.class
8 | GetPmPathThread.class
9 | GetAdbDevicesThread.class
10 | GetmCurrentFocusThread.class
11 | MainUITest$2.class
12 | deviceInfo.class
13 | MainUITest$3.class
14 | TopAPPInfo.class
15 | GetTopAPPBitThread.class
16 | MainUITest.class
17 | getDevices.class
18 | StreamCaptureThread.class
19 |
--------------------------------------------------------------------------------
/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst:
--------------------------------------------------------------------------------
1 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\DevicesTopAPP.java
2 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\getDevices.java
3 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\ScreenshotUtil.java
4 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\deviceInfo.java
5 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\TopAPPInfo.java
6 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\StreamCaptureThread.java
7 | C:\Users\Benjamin\Desktop\java\MobileDevicesTools\src\main\java\MainUITest.java
8 |
--------------------------------------------------------------------------------
/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
--------------------------------------------------------------------------------
/新增支持多设备选择功能.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Github-Benjamin/AndroidDevicesAPPInfo/0332de21f1f175e76efd29af07abec8c310642a3/新增支持多设备选择功能.png
--------------------------------------------------------------------------------