4

I'm trying to create an application that automatically downloads an apk from a specific server and install it on the system. My code for the installation looks like the following, but does not work.

File f = new File("/mnt/sdcard/download/", "Demo.apk");
Log.i("Demo", "f "+f.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package_archive");
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
m_context.startActivity(intent);            

Do i need to give any rights in Manifest.xml for installation? I know that question has been asked before, but none of the answers have helped me so far.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
user932865
  • 191
  • 2
  • 8
  • 1
    As i mentioned in my answer, use "package-archive" and not "package_archive". Hope this solves the ActivityNotFoundException. – Karthik Dec 13 '11 at 04:20

5 Answers5

8

This what I do in my case,

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive");
startActivity(intent);

And these are the permissions..

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 5
    still dont work, get this exception: *E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{foo.bar/foo.bar.FooActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Demo.apk typ=application/vnd.android.package_archive flg=0x10000000 }* – user932865 Dec 12 '11 at 13:08
  • is now shouln't be used, because it is intendent for system applications. – CoolMind Mar 11 '16 at 07:30
6

Thanks for all help, made it work at last. I share my working code and working Manifest.xml.

package test.installer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class InstallToolActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("Demo", "onCreate");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://"+"/mnt/sdcard/HelloWorld.apk"), "application/vnd.android.package-archive");
    intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }
}

Manifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.installer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".InstallToolActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

//Fredrik

user932865
  • 191
  • 2
  • 8
  • 1
    Hi Fredrik, I should thank you for sharing your experince on this. I have followed the exact same instructions, but I am getting "PArser Error. there is a problem parsing the package." I tried referring many forums but it was of not great help. Help of any sort is appreciated. – varunrao321 May 02 '12 at 15:39
  • Newbie, do you have made any progress on this issue? – Phil Apr 03 '13 at 07:28
  • @Newbie Are you using same signature for both software? And which android version are you using? – user932865 Apr 03 '13 at 08:33
3

i also do

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

and my install lookd like this

intent.setDataAndType(Uri.parse("file://"+path), "application/vnd.android.package-archive");

my path is a String, like your f

Boe-Dev
  • 1,585
  • 2
  • 14
  • 26
0

If your targetSdkVersion is equal or higher than 24, then you need to use FileProvider implementation for Android N and newer Android versions.

Here is the whole implementation:

// utility method
private void openAppInstaller(Context context, File toInstall) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", toInstall);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent)
    } else {
        Uri apkUri = Uri.fromFile(toInstall);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

Add provider FileProvider to AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

And create provider_paths.xml under xml resources.

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

More info about FileProvider configuration you can read here and here.

All credits go to @just_user because my answer based on his reply.

Oleksandr
  • 6,226
  • 1
  • 46
  • 54
0

You need to do this

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");

In your code, you have mentioned "package_archive", it should be "package-archive".
You would need the following permissions.

<uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Karthik
  • 3,509
  • 1
  • 20
  • 26