简体   繁体   English

从资源安装apk文件

[英]Installing apk file from resources

I want to write an Application that installs a new Application. 我想编写一个安装新应用程序的应用程序。 The .apk file for this new Application is included in the resources (R.raw.snake). 资源(R.raw.snake)中包含此新应用程序的.apk文件。 Can you give me the code for installing the snake.apk Application programmatically? 能否给我提供以编程方式安装snake.apk应用程序的代码? My current problem is that I cannot access the file as java.io.File but only as InputStream. 我当前的问题是我无法以java.io.File的形式访问文件,而只能以InputStream的形式访问。 I found this code: 我发现此代码:

File file = new File(this.getFilesDir() + File.separator + "Snake.apk");
try {
     InputStream inputStream = getResources().openRawResource(R.raw.snake);
     FileOutputStream fileOutputStream = new FileOutputStream(file);

     byte buf[]=new byte[1024];
     int len;
     while((len=inputStream.read(buf))>0) {
         fileOutputStream.write(buf,0,len);
     }

     fileOutputStream.close();
     inputStream.close();
} catch (IOException e1) {}
Installer installer = new Installer(this, file);
installer.install();

But I receive an error message on my tablet screen: 但是我在平板电脑屏幕上收到一条错误消息:

Parse error
There was a problem while parsing the package.

This is my Installer class: 这是我的安装程序类:

 package de.rbg.continental.bluetoothclient2;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.util.Log;

public class Installer {

private static Context context = null;
private static File file = null;
public static final String PACKAGE_NAME = "de.rbg.home.snake";
private static Receiver receiver = null;
private static final String TAG = "de.rbg.continental.nfcclient3.Installer";

public Installer(Context context, File file){
    Installer.context = context;
    Installer.file = file;
}

public boolean install(){
    if (!isAppInstalled()){
        registerReceiver();
        installApk();
        return true;
    }
    return false;
}

private boolean installApk(){
    try {
        Log.v(TAG, "installing file: " + file.getPath());
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

private boolean isAppInstalled(){
    List list = getListOfInstalledApps();
    for (int i = 0; i < list.size();i++){
        if (list.get(i).toString().contains(PACKAGE_NAME))
            return true;
    }
    return false;
}

private List getListOfInstalledApps(){
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    return context.getPackageManager().queryIntentActivities( mainIntent, 0);
}

private void registerReceiver(){
    receiver = new Receiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    context.registerReceiver(receiver, filter);
}

public static void unregisterReceiver(){
    context.unregisterReceiver(receiver);
}

public static void onApkInstalled(){
    unregisterReceiver();
}
}

Suggestions are appreciated. 建议表示赞赏。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() 
        + "<Path To Your APK>")), 
    "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Now I found out that the error was caused by the missing permissions in the manifest file: 现在,我发现错误是由清单文件中缺少权限引起的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM