Skip to content

Commit 40715f4

Browse files
committed
Added: 增加获取应用label和包名的对应关系
1 parent c273859 commit 40715f4

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

keyboardservice/src/main/AndroidManifest.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.android.adbkeyboard"
3-
android:versionCode="1"
4-
android:versionName="1.0" >
3+
android:versionCode="2"
4+
android:versionName="2.0" >
55

66
<uses-sdk
77
android:minSdkVersion="8"
@@ -29,7 +29,16 @@
2929
<action android:name="android.view.InputMethod" />
3030
</intent-filter>
3131
<meta-data android:name="android.view.im" android:resource="@xml/methods" />
32-
</service>
32+
</service>
33+
34+
<service
35+
android:name=".ProcessInfoService"
36+
android:exported="true">
37+
<intent-filter>
38+
<action android:name="com.android.adbkeyboard.ProcessInfo"/>
39+
</intent-filter>
40+
</service>
41+
3342
</application>
3443

3544
</manifest>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.android.adbkeyboard;
2+
3+
/**
4+
* Created by andrewleo on 2017/11/1.
5+
*/
6+
7+
import java.io.BufferedWriter;
8+
import java.io.File;
9+
import java.io.FileWriter;
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
import android.app.IntentService;
14+
import android.content.Intent;
15+
import android.content.pm.ApplicationInfo;
16+
import android.content.pm.PackageManager;
17+
import android.os.Bundle;
18+
import android.util.Log;
19+
20+
import org.json.JSONArray;
21+
import org.json.JSONObject;
22+
23+
public class ProcessInfoService extends IntentService {
24+
25+
private static final String PROCESS_INFO_ACTION = "com.android.adbkeyboard.ProcessInfo";
26+
private static final String DEFAULT_FILE_PATH = "/data/local/tmp/appinfos";
27+
28+
public ProcessInfoService() {
29+
super("ProcessInfoService");
30+
}
31+
32+
@Override
33+
protected void onHandleIntent(Intent intent) {
34+
if (intent != null) {
35+
Bundle bundle = intent.getExtras();
36+
String fileToSaved = (bundle != null) ? bundle.getString("fileToSave",
37+
DEFAULT_FILE_PATH) : DEFAULT_FILE_PATH;
38+
try {
39+
switch (intent.getAction()) {
40+
case PROCESS_INFO_ACTION:
41+
JSONArray jsonArray = new JSONArray();
42+
PackageManager pm = this.getPackageManager();
43+
for (ApplicationInfo appinfo : getAppInfos()) {
44+
JSONObject jsonObject = new JSONObject();
45+
jsonObject.put(appinfo.loadLabel(pm).toString(), appinfo.processName);
46+
jsonArray.put(jsonObject);
47+
}
48+
saveToFile(fileToSaved, jsonArray.toString());
49+
}
50+
} catch (Exception e) {
51+
Log.e("adbKeyBoard", e.getMessage());
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
}
57+
58+
private void saveToFile(String filePath, String content) throws IOException {
59+
File file = new File(filePath);
60+
if (!file.exists()) {
61+
file.createNewFile();
62+
}
63+
FileWriter fw = new FileWriter(file);
64+
BufferedWriter bw = new BufferedWriter(fw);
65+
bw.write(content);
66+
bw.close();
67+
}
68+
69+
private List<ApplicationInfo> getAppInfos() {
70+
PackageManager pm = getApplicationContext().getPackageManager();
71+
List<ApplicationInfo> appList = pm
72+
.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
73+
return appList;
74+
}
75+
}

0 commit comments

Comments
 (0)