0
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent); 

This was found here: Question [4967669] android-install-apk-programmatically

Using an android Service if do this I get the following error message:

05-03 08:24:14.559: E/AndroidRuntime(21288): FATAL EXCEPTION: Thread-1706 05-03 08:24:14.559: E/AndroidRuntime(21288): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I added the FLAG_ACTIVITY_NEW_TASK:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

But now nothing happens. No error but also no attempt to install the apk. I am thinking it might be because it has no activity (since it is a service)?

QUESTION Is it possible to install an APK via android background service? (if so) does any one knwo how do I do it?

Thanks in Advance

PS: From my understanding of services they are very much like activities so not sure why this wont work.

Community
  • 1
  • 1
Jake Graham Arnold
  • 1,416
  • 2
  • 19
  • 40
  • http://stackoverflow.com/questions/4604239/install-application-programmatically-on-android. Check the accepted answer in the link – Raghunandan May 03 '13 at 07:56
  • can u help me from which Link or resources u have taken help i am newbie and facing difficulty to achieve same target – nida Jan 16 '14 at 08:42

1 Answers1

3

You can install an APK from a background service. Try to use Uri.parse instead of Uri.fromFile

        File apkfile = new File(Environment.getExternalStorageDirectory() +
                  "/download/" + "app.apk");
        if (!apkfile.exists()) {
            return;
        }
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        installIntent.setDataAndType(
                Uri.parse("file://" + apkfile.toString()),
                "application/vnd.android.package-archive");
        startActivity(installIntent);
Mark Pope
  • 11,244
  • 10
  • 49
  • 59
buptcoder
  • 2,692
  • 19
  • 22
  • it does work from an service. My APK was actually corrupted and was showing a parsing message which was being hidden by an activity. Thanks. – Jake Graham Arnold May 03 '13 at 08:33