您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
91_如何通过代码安装一个apk文件
发布时间:2021-03-29 22:31:07编辑:雪饮阅读()
首先通过代码安装一个apk,这里并非是指命令行去安装,而是通过我们项目代码来实现,就好比有些应用商店安装软件一样,应用商店本身也是一个软件,这里实现的目标也就是类型应用商店来安装。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.packageinstaller">
<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/Theme.PackageInstaller">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.packageinstaller.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/sdcard/app-release.apk"
>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安装"
android:onClick="install"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Provider的依赖
主程 安装测试
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/com.example.packageinstaller/" />
<external-path
name="external_storage_root"
path="." />
</paths>
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private EditText et_path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = this.findViewById(R.id.et_path);
}
public void install(View view){
String path = et_path.getText().toString().trim();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
File apkFile=new File(path);
Uri uri=FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
}
关键字词:android,apk,安装,代码,意图
上一篇:90_获取手机sim卡的运营商