版本升级实现代码 点击下载安装 并在通知栏显示

本文介绍了一个安卓应用的更新流程,包括从服务器下载新的APK文件到本地存储,并通过通知栏显示下载进度。下载完成后,自动启动应用安装界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    private void loadNewApp() {
        String loadurl = "http://.........";//文件的网络路径
        final String store = "/sdcard"
                + "/apkname" + new Random(127) + UserUtils.getRoundChar(6) + ".apk";//下载的文件存储位置
        HttpUtils http = new HttpUtils();//使用xutils下载文件
        http.download(loadurl, store
                , true,
                new RequestCallBack<File>() {

                    @Override
                    public void onSuccess(
                            ResponseInfo<File> arg0) {
                        //取消通知
                        mNotificationManager.cancel(1);
                        //打开安装界面
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        File apkfile = new File(store);
                        intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onLoading(long total,
                                          long current, boolean isUploading) {
                        super.onLoading(total, current,
                                isUploading);
                        long process = current / total;
                        //开启通知
                        setUpNotification((int) process, (int) total);
                    }

                    @Override
                    public void onFailure(HttpException arg0,
                                          String arg1) {
                        rl_loading.setVisibility(View.INVISIBLE);
                    }
                });
    }<pre name="code" class="java">    private NotificationManager mNotificationManager;

    private void setUpNotification(int process, int max) {
        mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "...开始下载";
        long when = System.currentTimeMillis();
        Notification mNotification = new Notification(icon, tickerText, when);
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;

        RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.download_notification_layout);
        contentView.setTextViewText(R.id.name, "...正在下载...");
        contentView.setProgressBar(R.id.progressbar, max, process, true);
        mNotification.contentView = contentView;
//设置点击通知栏条目 跳转的activity
//        Intent intent = new Intent(activity, NewDianPuActivity.class);
//        PendingIntent contentIntent = PendingIntent.getActivity(activity, 0, intent,
//                PendingIntent.FLAG_UPDATE_CURRENT);

        // mNotification.contentIntent = contentIntent;
        mNotificationManager.notify(1, mNotification);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值