以编程方式安装APK

时间:2014-09-09 14:43:58

标签: android install apk

我使用的代码基本上与Android: install .apk programmatically相同。该应用程序启动新意图,但没有任何反应。 APK正在从我们的服务器上下载(内部应用程序不在Play商店中),但是如果我导航到下载文件夹并手动点击APK,则安装提示永远不会出现。是否与我在.Main活动上放置了intent过滤器这一事实有什么关系?

这是我的代码

        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>`

1 个答案:

答案 0 :(得分:1)

我想通了......拼错的机器人 - “application / vnd.andriod.package-archive”