日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 调用系统下载apk,如何在自己的App中调用Android系统自带的安装/卸载程序...

發布時間:2024/7/23 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 调用系统下载apk,如何在自己的App中调用Android系统自带的安装/卸载程序... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AppUtils里面寫了如何安裝和卸載apk(這段代碼出自這里:點擊打開鏈接~),這里的安裝和卸載調用的是Android本身的一個安裝卸載,所以可能頁面不會太優雅,并不符合商業App的期望,如果要做到更優雅的實現,就要用到靜默安裝/卸載,這個可以參照網上教程,我這里有一篇轉載還是很不錯的,大家可以參照一下:Android中實現靜態的默認安裝和卸載應用~

public class AppUtils {

/* 安裝apk */

public void installApk(Context context, String fileName) {

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setDataAndType(Uri.parse("file://" + fileName), "application/vnd.android.package-archive");

context.startActivity(intent);

}

/* 卸載apk */

public void uninstallApk(Context context, String packageName) {

// Uri uri = Uri.parse("package:" + packageName);

Uri uri = Uri.fromParts("package", packageName, null);

Intent intent = new Intent(Intent.ACTION_DELETE, uri);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);//一開始這句話是沒有的,運行的時候就報了這個錯誤“?android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity ?context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want??”,百度之后,得知,從一個Activity中要通過intent調出另一個Activity的話需要加上這個flag,所以各位切記,這句話不能漏

context.startActivity(intent);

}

}

在MainActivity中是這樣使用的,在這里,我以安裝企鵝為例,先把企鵝的apk放到了我自定義好的一個文件夾中:

public class MainActivity extends Activity {

AppUtils appUtils = new AppUtils();

String fileName = "";

String packageName = "";

Button btnCopyTp;

@BindView(R.id.btn_xz)

Button btnXz;

@BindView(R.id.btn_az)

Button btnAz;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

}

@OnClick({R.id.btn_xz, R.id.btn_az)

public void onClick(View view) {

switch (view.getId()) {

case R.id.btn_xz://卸載程序

String archiveFilePath = Environment.getExternalStorageDirectory() + "/MyFiles/Download/QQ_482.apk";//安裝包路徑

PackageManager pm = getPackageManager();

PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);

if (info != null) {

ApplicationInfo appInfo = info.applicationInfo;

packageName = appInfo.packageName; //得到安裝包名稱

xz(context, packageName);

}

break;

case R.id.btn_az://安裝程序

fileName = Environment.getExternalStorageDirectory() + "/MyFiles/Download/QQ_482.apk";

File file = new File(fileName);

if (file.exists()) {

az(context, fileName);

}

break;

}

}

public void az(Context context, String fileName) {

appUtils.installApk(context, fileName);

}

public void xz(Context context, String packageName) {

appUtils.uninstallApk(context, packageName);

}

}

布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.honey.mytest.activity.MainActivity">

android:text="通過我們的APP卸載其他應用"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btn_xz"

android:layout_below="@+id/btn_copy_tp"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_marginTop="56dp" />

android:text="通過我們的APP安裝其他應用"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/btn_xz"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:id="@+id/btn_az" />

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的android 调用系统下载apk,如何在自己的App中调用Android系统自带的安装/卸载程序...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。