相关疑难解决方法(0)

Android:以编程方式安装.apk

我在Android下载二进制文件问题 和以编程方式在Android上安装应用程序的帮助下做到了这一点 .

我想立即进行自动更新和自动安装.它是本地的,所以它是非市场应用.

这是我的代码:

public void Update(String apkurl){
    try {
        URL url = new URL(apkurl);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        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);

        InputStream 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();//till here, it works fine …
Run Code Online (Sandbox Code Playgroud)

android install

336
推荐指数
5
解决办法
33万
查看次数

设备所有者应用程序自动更新

是否可以静默更新设备所有者应用程序本身?我的应用程序设置为设备所有者,我希望它能够自动更新。是否可以?我正在使用的方法如下所示,

 private void install(Context context,String packageName,String apkPath){
    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();

    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(context.getPackageName());

    try {
        int sessionId = packageInstaller.createSession(params);
        PackageInstaller.Session session = packageInstaller.openSession(sessionId);
        OutputStream  out = session.openWrite(packageName,0,-1);
        File apkFile = new File(apkPath);
        InputStream in = new FileInputStream(apkFile);

        byte[] buffer = new byte[65536];
        int c;
        while ((c = in.read(buffer)) != -1) {
            out.write(buffer, 0, c);
        }
        session.fsync(out);
        in.close();
        out.close();

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,sessionId,
                new Intent("com.afwsamples.testdpc.INSTALL_COMPLETE"),0);
        IntentSender intentSender = pendingIntent.getIntentSender();

        session.commit(intentSender);

    }catch (Exception e){
        Log.d("PackageInstaller","package installer exception");
    } …
Run Code Online (Sandbox Code Playgroud)

android cosu

5
推荐指数
0
解决办法
1811
查看次数

标签 统计

android ×2

cosu ×1

install ×1