开发者

How to install APK automatically when the file download is complete (hosted on private)

I am new to android and i have been searching the web from last two days for this. I found following links too but i am not getting how and where to implement this code to start automatic installation of the apk file after it downloads completely and how to delete the downloaded apk file after installation. Please help me by guiding me the right way.

how to install apk file programmatically

Invoking activity from APK in another android application

Android install apk programmatically

Install APK programmatically on android

http://www.anddev.org/viewtopic.php?p=23928

EDIT :

i have written this code in manifest file :

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <!-- <uses-permission android:name="android.permission.INSTALL_PACKAGES"/> -->
    <application android:icon="@drawable/biz_logo"
        android:permission="android.permission.WRITE_CONTACTS">
        <activity android:name="com.biz.mlm.Main">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="content" />
                <data android:scheme="file" />
                <data andr开发者_Python百科oid:mimeType="application/vnd.android.package-archive" />
            </intent-filter>
        </activity>


Not possible. As some of those links point out, making an APK that automatically installs itself and deletes the original installer without any further user intervention is called malware.

You can write a program that will download and install arbitrary APKs if the user grants it the relevant permissions, but I'm yet to see a good implementation or documentation for this. Typically you can invoke the default system installer using code like this:

File apkFile = new File({path to APK});
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    startActivity(intent);


Have you tried adding the below permission?

<uses-permission
    android:name="android.permission.INSTALL_PACKAGES" />

Add the above code to your manifest (especially when you are getting security exception).

You can refer to Install apps silently, with granted INSTALL_PACKAGES permission

Hope this works for you.


Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File("/mnt/sdcard/myapkfile.apk");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);

The above code will install the myapkfile.apk. You just need to put this code in the onCreate() of the Activity.


Actually this is possible, use BroadcastReceiver to listen when package is added (PACKAGE_ADDED) in the system. Create BroadcastReceiver class, do the follow code :

public void onReceive(Context arg0, Intent arg1) {
    File downLoadApk = new File(Environment.getExternalStorageDirectory(),"New.apk");
        if (downLoadApk.exists()) {
            downLoadApk.delete();
        }

}


After Download give the path of apk file and it will install automatically in android

Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" +filename)), "application/vnd.android.package-archive"); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜