Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK
生活随笔
收集整理的這篇文章主要介紹了
Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
shell命令安裝
/*** 安裝apk** @param path apk文件路徑*/ public void installAPK(String path) {Log.i(TAG, "installAPK:" + path);com.dlc.xiaohaitun.utils.ShellUtils utils = new com.dlc.xiaohaitun.utils. ShellUtils();if (utils.isRoot()) {LogPlus.e("###已經(jīng)root, 開始 pm install ");utils.run("pm install -r " + path, 600 * 1000);} else {spm("沒有root");} }?
1、Android 8.0已上版本需要添加必要權(quán)限
依賴
implementation 'com.github.lovetuzitong:MultiImageSelector:1.2'?
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />?
2、適配Android 10.0所需xml文件——file_paths
?
?
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"><paths><!--external-path用來指定Uri共享的name屬性的值可以隨便填path屬性的值表示共享的具體路徑,這里設(shè)置為空代表將整個(gè)SD卡進(jìn)行共享,當(dāng)然你也可以共享存放的圖片地址--><external-path name="my_images" path=""/></paths></resources>?
3、適配Android 10.0所需provider
?
<!--authorities是自定義的,調(diào)用是復(fù)制即可--> <providerandroid:name="androidx.core.content.FileProvider"android:authorities="a.b.c.fileprovider"android:grantUriPermissions="true"android:exported="false"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /> </provider>?
4、根據(jù)Android版本,不同安裝配置
?
int version = android.os.Build.VERSION.SDK_INT; if (version > 24) {//android 7.0-10.0及以上版本installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive"); } else {//android 5.0-7.0installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);installIntent.setDataAndType(Uri.fromFile(updateFile), "application/vnd.android.package-archive");} startActivity(installIntent);?
5、完整demo,實(shí)現(xiàn)下載與安裝:https://download.csdn.net/download/meixi_android/12376770
?
6、在線交流解決下載安裝bug:QQ1085220040?
?
?
方法二
//在線更新 implementation ('com.teprinciple:updateapputilsx:2.3.0'){exclude group: 'org.jetbrains:annotations:13.0' }?
public void postApp(AppDownloadBean bean) {// if (!UserHelper.get().getSettingBean().isWifiUpdate()) {UpdateConfig updateConfig = new UpdateConfig();updateConfig.setNeedCheckMd5(false);updateConfig.setNotifyImgRes(R.mipmap.ic_launcher);updateConfig.setForce(bean.getEnforce() == 1); // updateConfig.setForce(true);updateConfig.setDebug(false);updateConfig.setAlwaysShowDownLoadDialog(true);UiConfig uiConfig = new UiConfig();uiConfig.setUiType(UiType.PLENTIFUL);uiConfig.setCustomLayoutId(R.layout.view_update_dialog_plentiful);UpdateAppUtils.getInstance().apkUrl(bean.getDownloadurl()).updateTitle(bean.getTitle()).updateContent(bean.getContent()).uiConfig(uiConfig).updateConfig(updateConfig).setOnInitUiListener(new OnInitUiListener() {@Overridepublic void onInitUpdateUi(@Nullable View view, UpdateConfig updateConfig, UiConfig uiConfig) {TextView mTitle = view.findViewById(R.id.tv_update_title);TextView mContent = view.findViewById(R.id.tv_update_content);mTitle.setText(bean.getTitle());mContent.setText(Html.fromHtml(bean.getContent()));}}).setUpdateDownloadListener(new UpdateDownloadListener() {@Overridepublic void onStart() {}@Overridepublic void onDownload(int progress) {}@Overridepublic void onFinish() {}@Overridepublic void onError(Throwable e) {}}).update();// }}/*** 獲取版本號** @return 當(dāng)前應(yīng)用的版本號*/public int getVersion(Activity activity) {try {PackageManager manager = activity.getPackageManager();PackageInfo info = manager.getPackageInfo(activity.getPackageName(), 0);return info.versionCode;} catch (Exception e) {e.printStackTrace();return 0;}}?
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayoutxmlns: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="280dp"android:layout_height="wrap_content"android:background="@drawable/bg_update_dialog"android:paddingBottom="10dp"><ImageViewandroid:id="@+id/iv_update_logo"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginTop="15dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:srcCompat="@drawable/ic_kzf"/><TextViewandroid:id="@+id/tv_update_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textColor="@color/text_title"android:textSize="16sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/iv_update_logo"tools:text="版本更新啦!"/><ScrollViewandroid:id="@+id/scrollView2"android:layout_width="match_parent"android:layout_height="90dp"android:layout_marginTop="10dp"android:overScrollMode="never"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_update_title"><TextViewandroid:id="@+id/tv_update_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:gravity="start"android:lineSpacingExtra="5dp"android:paddingLeft="20dp"android:paddingRight="20dp"android:textColor="@color/text_content"android:textSize="14sp"tools:text="1、快來升級最新版本\n2、這次更漂亮了\n3、快點(diǎn)來吧"/></ScrollView><TextViewandroid:id="@+id/btn_update_sure"android:layout_width="0dp"android:layout_height="35dp"android:layout_marginStart="20dp"android:layout_marginTop="10dp"android:layout_marginEnd="20dp"android:background="@drawable/bg_btn_lv_selector"android:gravity="center"android:text="@string/update_now"android:textColor="@color/white"android:textSize="14sp"app:layout_constraintBottom_toTopOf="@+id/btn_update_cancel"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/scrollView2"app:layout_goneMarginBottom="10dp"/><TextViewandroid:id="@+id/btn_update_cancel"android:layout_width="0dp"android:layout_height="35dp"android:layout_marginStart="20dp"android:layout_marginTop="5dp"android:layout_marginEnd="20dp"android:gravity="center"android:text="@string/update_cancel"android:textColor="@color/text_content"android:textSize="14sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/btn_update_sure"app:layout_goneMarginTop="10dp"/></androidx.constraintlayout.widget.ConstraintLayout>?
????"code":1,
????"msg":"app下載",
????"time":"1615343899",
????"data":{
????????"id":9,
????????"title":"Android回收員端",
????????"downloadurl":"http://szhdljhsxshg.app.xiaozhuschool.com/uploads/20200831/b995d216dc9297ca9350ac48ee7193a0.apk",
????????"newversion":"139",
????????"packagesize":"1",
????????"content":"<p>版本2升級測試</p>",
????????"enforce":0,
????????"brief":"版本更新啦!"
????}
}
總結(jié)
以上是生活随笔為你收集整理的Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nachos编译与使用--Nachos配
- 下一篇: linux高级IO