Jump to content

How to install apk from code?


fbdbdfb

Recommended Posts

Apk downloads normally, but when installing starts, 2d system window pops up so i can't do anything. How it can be fixed? Help please

This is installing script.

        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");

        string packageName = unityContext.Call<string>("getPackageName");
        string authority = packageName + ".provider";

        AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent");
        string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW");
        AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW);


        int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK");
        int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");

        AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
        AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
        AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);

        intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
        intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
        intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);

        currentActivity.Call("startActivity", intent);

 

Link to comment
Share on other sites

What do you mean by 2D system window?
Can you share a screenshot?

Also, a question to you, I'm using the exact same code that you are, but the fileProdiver returned from 

AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");

is just returning null for me.
Could you maybe share your aar/plugin/manifest setup?

Link to comment
Share on other sites

On 2/19/2022 at 3:32 PM, Paximillian said:

What do you mean by 2D system window?
Can you share a screenshot?

Can't do screenshot now. It's just a window on black background, that asks permission to install apk. I can't click yes/no cause controllers don't work in this mode. 

I had the same problem but i solved it by using Permission Manager. In this case i need FLAG_GRANT_READ_URI_PERMISSION, so i can't use it.

On 2/19/2022 at 3:32 PM, Paximillian said:

Could you maybe share your aar/plugin/manifest setup?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">
    <application android:icon="@drawable/app_icon"
                 android:label="@string/app_name"
                 android:theme="@style/Theme.WaveVR.Black"
                 android:requestLegacyExternalStorage="true"
				 tools:replace="android:theme">  <!--You can use your theme here.-->
        <activity android:name="com.htc.vr.unity.WVRUnityVRActivity"
                  android:label="@string/app_name"
				          android:configChanges="density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
                  android:enableVrMode="@string/wvr_vr_mode_component" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="com.htc.intent.category.VRAPP" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
            <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
        </activity>
      <provider android:name="androidx.core.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>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="vive.wave.vr.oem.data.OEMDataRead" />
    <uses-permission android:name="vive.wave.vr.oem.data.OEMDataWrite" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  
</manifest>

 

Link to comment
Share on other sites

  • 1 year later...

To install an APK (Android Package) file programmatically from your code, you can use an Intent to trigger the installation process. Here's how you can do it in Android:

 
java
 
import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import androidx.appcompat.app.AppCompatActivity; public class ApkInstallActivity extends AppCompatActivity { private static final int REQUEST_INSTALL_PERMISSION = 1001; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_apk_install); // Replace with the actual APK file path String apkFilePath = "file:///path/to/your/app.apk"; // Check if the device has the "Install Unknown Apps" permission if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !getPackageManager().canRequestPackageInstalls()) { // Request permission to install from unknown sources Intent permissionIntent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getPackageName())); startActivityForResult(permissionIntent, REQUEST_INSTALL_PERMISSION); } else { // Start the installation process installApk(apkFilePath); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_INSTALL_PERMISSION) { if (resultCode == RESULT_OK) { // Permission granted, start the installation process // Replace with the actual APK file path String apkFilePath = "file:///path/to/your/app.apk"; installApk(apkFilePath); } else { // Permission denied, handle accordingly } } } private void installApk(String apkFilePath) { Uri apkUri = Uri.parse(apkFilePath); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive"); installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(installIntent); } }

In this code, make sure to replace "file:///path/to/your/app.apk" with the actual path to your APK file. This code also handles the case where the device might require permission to install from unknown sources (a feature introduced in Android Oreo and later versions). It checks for the permission and requests it if necessary.

Remember to add the appropriate permissions and request the necessary permissions in your AndroidManifest.xml and runtime permissions flow as needed.

Please note that while this code demonstrates how to install an APK programmatically, it's important to consider user experience and security implications. Always make sure that you inform users about what you're doing and obtain any necessary permissions appropriately.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...