sunshineDade 2016-11-09 08:25 采纳率: 66.7%
浏览 2374
已采纳

Android 5.1 怎么修改源码packageinstaller 实现静默安装卸载

如标题所述,哪位大神做过这个需求,求指教怎么在源码上实现静默安装,感激。。。

  • 写回答

1条回答 默认 最新

  • AImmorta1 2016-11-12 06:51
    关注

    public class UpdateService extends Service {
    /** 安卓系统下载类 **/
    DownloadManager manager;

    /** 接收下载完的广播 **/
    DownloadCompleteReceiver receiver;
    
    /** 初始化下载器 **/
    private void initDownManager(String url) {
    
        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    
        receiver = new DownloadCompleteReceiver();
    
        //设置下载地址
        DownloadManager.Request down = new DownloadManager.Request(
                Uri.parse(url));
    
        // 设置允许使用的网络类型,这里是移动网络和wifi都可以
        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
                | DownloadManager.Request.NETWORK_WIFI);
    
        // 下载时,通知栏显示途中
        down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    
        // 显示下载界面
        down.setVisibleInDownloadsUi(true);
    
        // 设置下载后文件存放的位置
        down.setDestinationInExternalFilesDir(this,
                Environment.DIRECTORY_DOWNLOADS, "xiaozhuol.apk");
    
        // 将下载请求放入队列
        manager.enqueue(down);
    
        //注册下载广播
        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getExtras() != null){
            Bundle bundle = intent.getExtras() ;
            String url = bundle.getString("url") ;
            // 调用下载
            initDownManager(url);
        }
    
    
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
    
        return null;
    }
    
    @Override
    public void onDestroy() {
    
        // 注销下载广播
        if (receiver != null)
            unregisterReceiver(receiver);
    
        super.onDestroy();
    }
    
    // 接受下载完成后的intent
    class DownloadCompleteReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            //判断是否下载完成的广播
            if (intent.getAction().equals(
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
    
                //获取下载的文件id
                long downId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    
                //自动安装apk
                installAPK(manager.getUriForDownloadedFile(downId));
    
                //停止服务并关闭广播
                UpdateService.this.stopSelf();
    
            }
        }
    
        /**
         * 安装apk文件
         */
        private void installAPK(Uri apk) {
    
            // 通过Intent安装APK文件
            Intent intents = new Intent();
    
            intents.setAction("android.intent.action.VIEW");
            intents.addCategory("android.intent.category.DEFAULT");
            intents.setDataAndType(apk,"application/vnd.android.package-archive");
            intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 如果不加上这句的话在apk安装完成之后点击单开会崩溃
    
            getApplication().startActivity(intents);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格