6

I installed an apk which was saved in directory of /data/data/package_name/files with codes below:

Uri uri = Uri.fromFile(new File(apkSavingPath));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"application/vnd.android.package-archive");
mContext.startActivity(intent);

I want it to return a resultcode to tell me whether the apk installed successfully or not, and I have tried method of startActivityForResult, but it didn't work.

On method of onActivityResult, it's resultCode is always 0(zero) whether apk installed successfully or not. Can I get such a resultcode?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
stardust
  • 405
  • 1
  • 6
  • 15
  • The simple answer is "you cannot". Installer activity doesn't return any result. – Aleks G Apr 22 '13 at 09:28
  • possible duplicate of [Install APK programmatically on android](http://stackoverflow.com/questions/6362479/install-apk-programmatically-on-android) – Victor Ronin Sep 19 '13 at 19:03

1 Answers1

5

try this.

Intent intent = new Intent(Intent.ACTION_VIEW);           
intent.setDataAndType("ApkFilePath...","application/vnd.android.package-archive");
activity.startActivityForResult(intent,5000);

Add your receiver on AndroidManifest.xml

<receiver android:name=".PackageReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

This class then gets called when a new package is installed:

public class PackageReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // handle install event here
  }
}
Bob
  • 22,810
  • 38
  • 143
  • 225
Imran Ali
  • 539
  • 4
  • 17
  • 1
    Thanks! it worked. But after adding `android:enabled="true" android:exported="true"`. I edited your answer. – Bob Oct 02 '13 at 07:47