Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get a resultcode when i use 'startActivity' to install an apk

Tags:

android

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?

like image 631
stardust Avatar asked Apr 22 '13 09:04

stardust


1 Answers

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
  }
}
like image 198
Imran Ali Avatar answered Oct 06 '22 00:10

Imran Ali