Josh Elias
Josh Elias

Reputation: 3290

How to monitor the life cycle of an activity outside an activity class

I'm trying to write a manager that downloads and installs an apk from the web for a terminal service I'm doing.

I can have my APKManager accept a context and use it to start an activity to install the apk after it's been downloaded.

public static void installAPK(Context context, String filename, Callback callback) {
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        File apkFile = new File(Environment.getExternalStoragePublicDirectory(
                ENVIRONMENT_DIRECTORY), DIRECTORY_NAME +"/"+filename);
        installIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(installIntent);
}

How can I get a completion callback from the context.startActivity()? I understand that you can receive it in this function that is available from a child of activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

But my manager is not an activity so I'm hoping there is another way.

Upvotes: 0

Views: 148

Answers (1)

K D
K D

Reputation: 104

Listen to broadcast receiver for action android:name="android.intent.action.PACKAGE_ADDED"

Reference : Install APK programmatically on android

Upvotes: 1

Related Questions