user2919966
user2919966

Reputation: 21

Installing apk programmatically

I'm trying to find best solution for catch moment when user closed app installation menu. If user press OK button and app was installed success, the intent PACKAGE_ADDED was sent but how to catch CANCEL installation button?

I think about some flags on onStop, onPause and onResume functions but I think it is not right way.

PS : Also If application has system permission PSS: I think different workaround like abstract observer is not suitable. May I know what is the correct way to achieve my objective?

Upvotes: 1

Views: 358

Answers (1)

Veaceslav Gaidarji
Veaceslav Gaidarji

Reputation: 4301

You can monitor current top Activity, and check if it is installer Activity. Also register for actions like PACKAGE_ADDED, for monitoring installation progress. If user opened PackageInstallerActivity, then returned to ManageApplications activity, and you haven't received PACKAGE_ADDED action - then your application wasn't installed and that's the Cancel button action. That's all what you can do. There is no pre-install action sent by the system.

class MonitorActivities extends Thread{

boolean exit = false;
ActivityManager am = null;
Context context = null;

public MonitorActivities (Context context){
    this.context = context;
    am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){
    Looper.prepare();

    while(!exit){

        // Return a list of the tasks that are currently running,
        // with the most recent being first and older ones after in order.
        // Taken 1 inside getRunningTasks method means want to take only
        // top activity from stack and forgot the olders.
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

        String activityName = taskInfo.get(0).topActivity.getClassName();

        Log.i("topActivity", "CURRENT Activity ::" + activityName);

        if(activityName.equals("com.android.packageinstaller.PackageInstallerActivity")) {
            // User is currently in application installation process

            exit = true;
        } else if(activityName.equals("com.android.settings.ManageApplications")) {
            // user has been taken back to Manage Applications window
            // we should close the activity monitoring now
            exit=true;
        }
    }
    Looper.loop();
}
}

Upvotes: 1

Related Questions