Skip to content

Instantly share code, notes, and snippets.

@Fuzion24
Created May 6, 2012 16:53
Show Gist options
  • Save Fuzion24/2623253 to your computer and use it in GitHub Desktop.
Save Fuzion24/2623253 to your computer and use it in GitHub Desktop.
Code that will install and remove apks from an Android device without user interaction
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packagemanager.poc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PackageManagerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/fuzion24/Development/workspace/PackageManager/src/android/content/pm/IPackageDeleteObserver.aidl
*/
package android.content.pm;
/**
* API for deletion callbacks from the Package Manager.
*
* {@hide}
*/
public interface IPackageDeleteObserver extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver
{
private static final java.lang.String DESCRIPTOR = "android.content.pm.IPackageDeleteObserver";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an android.content.pm.IPackageDeleteObserver interface,
* generating a proxy if needed.
*/
public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof android.content.pm.IPackageDeleteObserver))) {
return ((android.content.pm.IPackageDeleteObserver)iin);
}
return new android.content.pm.IPackageDeleteObserver.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_packageDeleted:
{
data.enforceInterface(DESCRIPTOR);
boolean _arg0;
_arg0 = (0!=data.readInt());
this.packageDeleted(_arg0);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements android.content.pm.IPackageDeleteObserver
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(((succeeded)?(1):(0)));
mRemote.transact(Stub.TRANSACTION_packageDeleted, _data, null, android.os.IBinder.FLAG_ONEWAY);
}
finally {
_data.recycle();
}
}
}
static final int TRANSACTION_packageDeleted = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException;
}
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/fuzion24/Development/workspace/PackageManager/src/android/content/pm/IPackageDeleteObserver.aidl
*/
package android.content.pm;
/**
* API for deletion callbacks from the Package Manager.
*
* {@hide}
*/
public interface IPackageDeleteObserver extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver
{
private static final java.lang.String DESCRIPTOR = "android.content.pm.IPackageDeleteObserver";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an android.content.pm.IPackageDeleteObserver interface,
* generating a proxy if needed.
*/
public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof android.content.pm.IPackageDeleteObserver))) {
return ((android.content.pm.IPackageDeleteObserver)iin);
}
return new android.content.pm.IPackageDeleteObserver.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_packageDeleted:
{
data.enforceInterface(DESCRIPTOR);
boolean _arg0;
_arg0 = (0!=data.readInt());
this.packageDeleted(_arg0);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements android.content.pm.IPackageDeleteObserver
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(((succeeded)?(1):(0)));
mRemote.transact(Stub.TRANSACTION_packageDeleted, _data, null, android.os.IBinder.FLAG_ONEWAY);
}
finally {
_data.recycle();
}
}
}
static final int TRANSACTION_packageDeleted = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException;
}
package com.packagemanager.poc;
import java.io.File;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.net.Uri;
public class PackageManagerActivity extends Activity {
public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
String fileName = "/sdcard/test.apk";
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
installPackage(fileName);
}
public void installPackage(String fileName){
try {
PackageManager pkgManager = getPackageManager();
Method installPackage = pkgManager.getClass().getMethod("installPackage", android.net.Uri.class, IPackageInstallObserver.class ,int.class,java.lang.String.class);
Uri androidAPK = Uri.fromFile(new File(fileName));
Object[] params = new Object[] { androidAPK, null, INSTALL_REPLACE_EXISTING, "" };
installPackage.invoke(pkgManager, params);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deletePackage(String packageName){
try {
PackageManager pkgManager = getPackageManager();
Method deletePackage = pkgManager.getClass().getMethod("installPackage",String[].class, IPackageDeleteObserver.class, int.class);
Object[] params = new Object[] {packageName, null, 0 };
deletePackage.invoke(pkgManager, params);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@shahinyotech
Copy link

i am using rooted device but its not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment