Skip to content

Commit

Permalink
Added: 增加获取应用label和包名的对应关系
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleo committed Nov 1, 2017
1 parent c273859 commit 40715f4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
15 changes: 12 additions & 3 deletions keyboardservice/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.adbkeyboard"
android:versionCode="1"
android:versionName="1.0" >
android:versionCode="2"
android:versionName="2.0" >

<uses-sdk
android:minSdkVersion="8"
Expand Down Expand Up @@ -29,7 +29,16 @@
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/methods" />
</service>
</service>

<service
android:name=".ProcessInfoService"
android:exported="true">
<intent-filter>
<action android:name="com.android.adbkeyboard.ProcessInfo"/>
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.android.adbkeyboard;

/**
* Created by andrewleo on 2017/11/1.
*/

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import android.app.IntentService;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

public class ProcessInfoService extends IntentService {

private static final String PROCESS_INFO_ACTION = "com.android.adbkeyboard.ProcessInfo";
private static final String DEFAULT_FILE_PATH = "/data/local/tmp/appinfos";

public ProcessInfoService() {
super("ProcessInfoService");
}

@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
Bundle bundle = intent.getExtras();
String fileToSaved = (bundle != null) ? bundle.getString("fileToSave",
DEFAULT_FILE_PATH) : DEFAULT_FILE_PATH;
try {
switch (intent.getAction()) {
case PROCESS_INFO_ACTION:
JSONArray jsonArray = new JSONArray();
PackageManager pm = this.getPackageManager();
for (ApplicationInfo appinfo : getAppInfos()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(appinfo.loadLabel(pm).toString(), appinfo.processName);
jsonArray.put(jsonObject);
}
saveToFile(fileToSaved, jsonArray.toString());
}
} catch (Exception e) {
Log.e("adbKeyBoard", e.getMessage());
e.printStackTrace();
}
}

}

private void saveToFile(String filePath, String content) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}

private List<ApplicationInfo> getAppInfos() {
PackageManager pm = getApplicationContext().getPackageManager();
List<ApplicationInfo> appList = pm
.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
return appList;
}
}

0 comments on commit 40715f4

Please sign in to comment.