gpt4 book ai didi

java - 从资源安装 apk 文件

转载 作者:行者123 更新时间:2023-11-30 02:40:13 25 4
gpt4 key购买 nike

我想编写一个安装新应用程序的应用程序。这个新应用程序的 .apk 文件包含在资源 (R.raw.snake) 中。你能给我以编程方式安装 snake.apk 应用程序的代码吗?我当前的问题是我无法将文件作为 java.io.File 访问,而只能作为 InputStream 访问。我找到这段代码:

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();

但我在平板电脑屏幕上收到一条错误消息:

Parse error
There was a problem while parsing the package.

这是我的安装程序类:

 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();
}
}

欢迎提出建议。

最佳答案

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);

关于java - 从资源安装 apk 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com