Android 以编程方式安装APK

Android 以编程方式安装APK,android,installation,apk,Android,Installation,Apk,我使用的代码基本上与。应用程序启动了新的意图,但什么也没发生。APK正在从我们的服务器上被拉下来(内部应用程序不在Play store上),但是如果我导航到downloads文件夹并手动单击APK,安装提示将永远不会出现。这是否与我在.Main活动上放置了意图过滤器这一事实有关 <application android:allowBackup="true" android:icon="@drawable/bancsource" android:label="@str

我使用的代码基本上与。应用程序启动了新的意图,但什么也没发生。APK正在从我们的服务器上被拉下来(内部应用程序不在Play store上),但是如果我导航到downloads文件夹并手动单击APK,安装提示将永远不会出现。这是否与我在.Main活动上放置了意图过滤器这一事实有关

<application
    android:allowBackup="true"
    android:icon="@drawable/bancsource"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:path="@string/updatePath"/>
            <data android:mimeType="application/vnd.andriod.package-archive"/>


        </intent-filter>
    </activity>`
这是我的密码

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        URL url = new URL("http://ourURL/app.apk");

        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.connect();

        String PATH = Environment.getExternalStorageDirectory() + "/Download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "app.apk");
        FileOutputStream fos = new FileOutputStream(outputFile);

       is = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();




        Intent promptInstall = new Intent(Intent.ACTION_VIEW);
                promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + "app.apk")), "application/vnd.andriod.package-archive");
                promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);



        startActivity(promptInstall);

    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Update error!" + e.toString() + e.getStackTrace().toString(), Toast.LENGTH_LONG).show();
        TextView txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
        txtQuestion.setText(e.toString());
    }
}`
<application
    android:allowBackup="true"
    android:icon="@drawable/bancsource"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:path="@string/updatePath"/>
            <data android:mimeType="application/vnd.andriod.package-archive"/>


        </intent-filter>
    </activity>`
这是我的清单

<application
    android:allowBackup="true"
    android:icon="@drawable/bancsource"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:path="@string/updatePath"/>
            <data android:mimeType="application/vnd.andriod.package-archive"/>


        </intent-filter>
    </activity>`

`

我发现了…android在“application/vnd.andriod.package archive”中拼写错误。

向清单添加权限:我拥有所有这些权限。很抱歉,我的代码示例中没有包含它们。
<application
    android:allowBackup="true"
    android:icon="@drawable/bancsource"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.OPENABLE"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:path="@string/updatePath"/>
            <data android:mimeType="application/vnd.andriod.package-archive"/>


        </intent-filter>
    </activity>`