java - 从java代码在android设备中安装apk
问题描述
我正在尝试用 Java 制作一个简单的应用程序,以便在通过 USB 连接的 Android 设备上安装 APK。手动使用 ABD 或从 Android Studio 安装它工作正常,但我想在我的应用程序中提供一个简单的单击安装选项,我尝试了以下代码,但不幸的是,它不起作用
abdsourcesync = apkpath;
progress.setString("sync in progress");
System.out.println("Starting Sync via adb with command " + "adb"
+ " install -r " + apkpath);
Process process = Runtime.getRuntime().exec(
"adb" + " install -r " + apkpath);
InputStreamReader reader = new InputStreamReader(
process.getInputStream());
Scanner scanner = new Scanner(reader);
scanner.close();
int exitCode = process.waitFor();
System.out.println("Process returned: " + exitCode);
我在这里搜索过,但我只发现从 Android 应用程序或 android studio 中安装 APK,而不是从核心 Java 安装 APK。或 java web 模块
您的帮助将不胜感激;
解决方案
不要忘记运行时权限
这个简单的示例适用于 API 28。它会打开一个 apk 文件以从“下载文件夹”安装
为简化起见: 将要安装的应用程序的 apk 文件下载到手机的“下载”文件夹中。(有很多以编程方式进行的说明,您可以手动进行)
去做
- 创建新项目
- 向 MainActivity 添加一个按钮
- 在 res 文件夹中创建 xml 文件夹并在那里创建一个 file_paths.xml 文件
- 使用下面的代码
- 享受 =)
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.teko.testcleanopenfile">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
主要活动
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// textView and button
textView = findViewById(R.id.textView);textView.setText("Hello updatable World\n");
(findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {RunAPK(getBaseContext());}
});
}
private void RunAPK(Context context){
requestPermissionsToRead();
}
private void requestPermissionsToRead() {
// ASK RUNTIME PERMISSIONS
ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE},111);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults.length > 0) {
if (requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
textView.append("Permission granted write\n");
// Create Uri
File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file1 = new File (downloads + "//app-debug.apk");//downloads.listFiles()[0];
Uri contentUri1 = getUriForFile(this, BuildConfig.APPLICATION_ID, file1);
// Intent to open apk
Intent intent = new Intent(Intent.ACTION_VIEW, contentUri1);
intent.setDataAndType(contentUri1, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
}
}
}
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="download" path="."/>
</paths>
推荐阅读
- ios - 需要有关自动布局的帮助
- node.js - Nodejs没有在服务器中运行
- android - Ionic 如何从 ble 外围设备读取数据
- javascript - 砌体网格在负载时坍塌
- javascript - 如何正确捕获 axios get 请求中的错误?
- python - 如何在 python 中使用 opencv 从 ip camera 访问视频流?
- php - PhpStorm - 未定义类 FPDF
- ssl - 与hue oozie服务器握手错误
- javascript - 组件不会在路由更改时重新渲染 - React HashRouter
- elasticsearch - 如何使用 MultiTermVectorsAsync