美文网首页android 技术知识
Android 中实现点击下载跳转其他APK功能

Android 中实现点击下载跳转其他APK功能

作者: 追梦小乐 | 来源:发表于2017-08-09 11:42 被阅读202次

    开发过程中肯定会遇到这样的一个功能:点击图标下载apk,安装后实现功能跳转,现简单记录一下,以防以后用上。

    1、先判断当前手机或者平板有没有安装该apk

        /**
         * 判断有没有安装该apk
         * @param packageName
         * @param context
         * @return
         */
        public static boolean isAvilible(String packageName,Context context) {
            final PackageManager packageManager = context.getPackageManager();
            // 获取所有已安装程序的包信息
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
            for (int i = 0; i < pinfo.size(); i++) {
                if (pinfo.get(i).packageName.equalsIgnoreCase(packageName))
                    return true;
            }
            return false;
        }
    

    2、复制assert目录下的apk到sd卡中(该方式实现的是:要下载的apk先放到assert目录下)

    /**
         * 复制assert目录下的apk到sd卡中
         * @param context
         * @param apkName
         * @return
         */
        public static boolean copyApkFromAssets(Context context,String apkName) {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+apkName;
            boolean copyIsFinish = false;
            try {
                InputStream is = context.getAssets().open(apkName);
                File file = new File(path);
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                byte[] temp = new byte[1024];
                int i = 0;
                while ((i = is.read(temp)) > 0) {
                    fos.write(temp, 0, i);
                }
                fos.close();
                is.close();
                copyIsFinish = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return copyIsFinish;
        }
    

    3、提示用户是否要安装

     /**
         * 弹窗提示用户安装与否
         * @param apkName
         */
        private void showInstallApkOrNo(final String apkName){
            if (AppUtils.copyApkFromAssets(getActivity(), apkName)) {
                Dialog builder = new AlertDialog.Builder(getActivity())
                        .setIcon(R.drawable.ic_launcher)
                        .setMessage("是否安装?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+apkName), "application/vnd.android.package-archive");
                                getActivity().startActivity(intent);
                            }
                        })
                        .create();
                builder.show();
            }
        }
    

    4、实现功能跳转

    private void jumpToIntegrationFunction(int position){
            String apkName = "app-release.apk";
            String packageName = "com.tecsun.tsb.integration.hubei";
            String srcActivity = "com.tecsun.tsb.integration.hubei.activity.MainActivity";
            
            if ( !AppUtils.isAvilible( packageName , getActivity( ) ) ){
    //          LogUtils.v( "com.tecsun.tsb.integration.hubei not installed" );
    //          return;
                showInstallApkOrNo(apkName);
                return;
            }
            
            int type = -1;
            if ( position == 0 ) {
                type = TQ_APPLY;
            }
            if ( position == 11 ) {     
                type = TQ_INTRODUCE;
            }
            if ( type > 0 ) {
                Intent intent = new Intent( );
                srcActivity = packageName + ".activity.MainActivity";
                ComponentName componentName = new ComponentName( packageName ,
                        srcActivity );
                intent.setComponent( componentName );
                intent.putExtra( "type" , type );
                intent.putExtra( "TOKEN_ID" , AppApplication.TOKEN_ID );
                intent.setAction( srcActivity );
                getActivity( ).startActivity( intent );
                return;
            }
        }
    

    相关文章

      网友评论

        本文标题:Android 中实现点击下载跳转其他APK功能

        本文链接:https://www.haomeiwen.com/subject/pfvfrxtx.html