Skip to content

Instantly share code, notes, and snippets.

@Androguide
Last active December 17, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Androguide/5532166 to your computer and use it in GitHub Desktop.
Save Androguide/5532166 to your computer and use it in GitHub Desktop.
This snippet returns all package names matching a certain word from all the apps installed on an Android device.
// This shell command returns all packages that contain the word camera
String command = "pm list packages -f > "
+ Environment.getExternalStorageDirectory() + "/packages.txt\n"
+ "grep camera " + Environment.getExternalStorageDirectory()
+ "/packages.txt\n";
List<String> cameras = new ArrayList<String>();
try {
String line;
Process process = Runtime.getRuntime().exec("sh");
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
// execute the command
stdin.write((command).getBytes());
stdin.write("exit\n".getBytes());
stdin.flush();
stdin.close();
// read output
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while ((line = br.readLine()) != null) {
// add each found package to our List
cameras.add(line);
// log the output for ease of debugging/bug reports
Log.d("[Output]", line);
}
br.close();
// read & log errors if any
br = new BufferedReader(new InputStreamReader(stderr));
while ((line = br.readLine()) != null) {
Log.e("[Error]", line);
}
br.close();
process.waitFor();
process.destroy();
} catch (Exception ignored) {}
// Do something with your List values, such as starting your intent
// Make sure you have declared the WRITE_EXTERNAL_STORAGE permission in
// your manifest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment