Atul O Holic
Atul O Holic

Reputation: 6792

Delete Apk after app installation

I am working on an app in which I have to restrict the user to share the apk personally. One can download it but should not be allowed to share the App (apk) personally via bluetooth etc.

I searched and read alot about the same issue but didn't find a complete answer. I have tried the below.

  1. How to get the file *.apk location in Android device

  2. Download, Installing and Delete .apk file on android device programmatically from a website other than marketplace

What I have understood from above is that the easy way to do this is to delete the apk file from the User's device after the app is installed. (I also know that user may generate this apk again via root access but that is fine as at-least I will be able to restrict not all but many).

Now what I have tried, I am registering a Broadcast receiver and in my onReceive() method I have the below,

PackageManager pm = getPackageManager();

for (ApplicationInfo app : pm.getInstalledApplications(0)) {
 if (myPackage.equalsIgnoreCase(app.packageName)) {
    apkPath = app.sourceDir;
    break;
 }
}
Log.d("Package", myPackage + " - APK - " + apkPath);
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, apkPath);
    file.delete();
}

But this does nothing as I am not aware of the IntentFilter I should use to register this.

Can any one guide me on what is the next step? and in case I am completely on a wrong land then help me achieve the above?

Any help will be appreciated. :)

Upvotes: 4

Views: 3812

Answers (6)

AkiAki007
AkiAki007

Reputation: 429

You can also use the HockeyApp product and distribute using the Android App that they provide, as well as enable the "restriction" features. I'm pretty sure that will do what you need without having to write extra code.

Upvotes: 0

V.P.
V.P.

Reputation: 612

Sorry for bad english.

Here my solution not actual code:

1) set your broadcastreciver when any new application is install in your device.

2) When your broadcastreciver is call, that time you write code in onRecive() method check the your app package if install or not

   private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}

using this method or any if you get true means your application is install

3) if you get true from isPackageInstalled("com.download.app", context) method that time delete apk file from your SD card or any loaction in device

if you more clarification i will be explain.

Upvotes: 1

sisir
sisir

Reputation: 111

i used the idea of creating a folder when the app runs for the first time .if it doesnot exist , delete the apk. if the folder exists , then the apk has already been deleted. This folder will be deleted on its own if the app is unstalled.

public static String link = Environment.getExternalStorageDirectory()
        .getPath() + "/Android/data/YourAppPackageName/";

//check if its running for the first time
    File f = new File(link);
    if (f.exists()) {

       File f1 = new File("/data/app/YourAppPackageName.apk")
       if(f1.exists()){f1.delete();}
    }
    else {
    f.mkdirs();
    }

Upvotes: 0

nikhil.thakkar
nikhil.thakkar

Reputation: 1093

You can use the Android App Licensing Service here. Refer http://developer.android.com/google/play/licensing/index.html and the implmentation here http://developer.android.com/google/play/licensing/adding-licensing.html.

This will ensure that the apk cannot be shared(making it protected) using any of the apps available on market.

Upvotes: 2

Rajiv Manivannan
Rajiv Manivannan

Reputation: 504

As far as I know, It is not possible to listen the installation complete broadcast event. Android Api having Broadcast Action: "ACTION_PACKAGE_ADDED" But newly installed package does not receive this broadcast. Source : Here

My suggestion is check in your first launcher activity and delete the apk.

Upvotes: 2

scottt
scottt

Reputation: 8371

On later versions of Android, some (all?) broadcasts aren't sent to an app until it has been manually opened at least once. This is a security feature that can't be circumvented.

Upvotes: 2

Related Questions