mono for android software自动更新

本文介绍如何使用MonoforAndroid实现应用的自动更新功能,包括获取版本信息、与服务器对比版本、下载新版本并安装的过程。
Mono For Android 这个对很多人来说似乎很陌生,因为在国内还不是很流行,所以国内资料也很少关于mono for Android,甚至国外也很少资料。

好了言归正传,直奔主题

最近为了完成项目最后的一个大功能,那就是如果软件有更新怎么办?那肯定是要做到自动提示更新了。

如果是Android的话这个网上资料一堆,直接copy,但是mono for android压根就没有,所以我只能参考java代码用C#写了。下面是我的资料:
1.在AndroidManifest.xml文件中加入这个

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="com.myapp" android:versionName="3.0.0.0">

每次只需要修改versionName就好。(package 的名字不需要修改,否则会出错,我试过了)

2.下面是定义一个Global.cs文件,用于读取当前app的版本号。

 public class Global
    {
       //Vision information
       public static string getVersion(Context context)
       {
           string version = "";
           try
           {
               version = context.PackageManager.GetPackageInfo("com.myapp", 0).VersionName;
           }
           catch (Exception exception)
           {
               MessageBox.Show(context,"CONFIRM",exception.Message);//这是本人写的一个messagebox
           }
           return version;
       }
    }
3.和服务器版本的对比。

这个随意你怎么对比了,我是直接把版本号发到服务器上,服务器有存储当前最新版本的版本号,如果相等就不管,否则就提示更新。

4.下载文件,更新app(这个我用了三天才搞定,断断续续)。

public class UpdateManager
    {
        private Context mContext;


        //返回的安装包url  
        private string apkUrl = "http://localhost/msoft/mars.apk";


        private Dialog downloadDialog;
        /* 下载包安装路径 */
        private static string savePath = "/sdcard/Download/";


        private static string saveFileName = savePath + "mars.apk";


        /* 进度条与通知ui刷新的handler和msg常量 */
        private ProgressBar mProgress;


        private int progress;


        private bool interceptFlag = false;


        public UpdateManager(Context context)
        {
            this.mContext = context;
        }

        public  void showDownloadDialog() 
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.SetTitle("Upgrade sofware");
            LayoutInflater inflater = LayoutInflater.From(mContext);
            View v = inflater.Inflate(Resource.Layout.progress,null);
            mProgress = (ProgressBar)v.FindViewById(Resource.Id.progress);
            builder.SetView(v);
            builder.SetNegativeButton("Cancel",new MyClickListener());
            interceptFlag = MyClickListener.interceptFlag;
            downloadDialog = builder.Create();
            downloadDialog.Show();
            Task.Factory.StartNew(() => downloadApk());
        }
        private void downloadApk() 
        {
            try
            {
                URL url = new URL(apkUrl);
               
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestProperty("User-Agent","PacificHttpClient");
                conn.Connect();
                conn.ConnectTimeout = 10000;
                conn.ReadTimeout = 20000;
                int length = conn.ContentLength;
                System.IO.Stream stream = conn.InputStream;              
                File file = new File(savePath);
                if (!file.Exists())
                {
                    file.Mkdir();
                }
                string apkFile = saveFileName;
                File ApkFile = new File(apkFile);
                FileOutputStream fos = new FileOutputStream(ApkFile);
                int count = 0;
                int intBufferSize = 16384 * 8;//这个Size随意你喜欢。
                byte[] bytBuffer = new byte[intBufferSize];
                interceptFlag = false;
                int numread = 0;
                while ((numread = stream.Read(bytBuffer, 0, intBufferSize)) > 0)
               {
                    count += numread;
                    progress = (int)(((float)count / length) * 100);
                    mProgress.Progress = progress;
                    fos.Write(bytBuffer,0,numread);
                }
                if (numread == 0)
                   Task.Factory.StartNew(()=> installApk());
                fos.Close();
                stream.Close();
            }
            catch (MalformedURLException exception)
            {
                exception.PrintStackTrace();
            }
        }
        private void installApk()
        {
            downloadDialog.Dispose();
            File apkfile = new File(saveFileName);
            if (!apkfile.Exists())
            {
                return;
            }
            var targetUri = Android.Net.Uri.FromFile(apkfile);
            Intent i = new Intent(Intent.ActionView);
            i.SetDataAndType(targetUri, "application/vnd.android.package-archive");
            mContext.StartActivity(i);
        }  
    }

    class MyClickListener : Java.Lang.Object, IDialogInterfaceOnClickListener 
    {
        public static bool interceptFlag = false;
        public void OnClick(IDialogInterface dialog,int which) 
        {
            dialog.Dismiss();
            interceptFlag = true;
        }
    }

好了,到此为止,更新app功能完成了。我觉得比java的简单了。希望对你有所帮助吧,不明白的可以留言哦!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- Copyright (c) 2016-2020, The Linux Foundation. All rights reserved Not a Contribution. --> <!-- Copyright (C) 2015 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Changes from Qualcomm Innovation Center are provided under the following license: Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <audioPolicyConfiguration version="7.0" xmlns:xi="http://www.w3.org/2001/XInclude"> <!-- version section contains a “version” tag in the form “major.minor” e.g version=”1.0” --> <!-- Global configuration Decalaration --> <globalConfiguration speaker_drc_enabled="true" call_screen_mode_supported="true"/> <!-- Modules section: There is one section per audio HW module present on the platform. Each module section will contains two mandatory tags for audio HAL “halVersion” and “name”. The module names are the same as in current .conf file: “primary”, “A2DP”, “remote_submix”, “USB” Each module will contain the following sections: “devicePorts”: a list of device descriptors for all input and output devices accessible via this module. This contains both permanently attached devices and removable devices. “mixPorts”: listing all output and input streams exposed by the audio HAL “routes”: list of possible connections between input and output devices or between stream and devices. "route": is defined by an attribute: -"type": <mux|mix> means all sources are mutual exclusive (mux) or can be mixed (mix) -"sink": the sink involved in this route -"sources": all the sources than can be connected to the sink via vis route “attachedDevices”: permanently attached devices. The attachedDevices section is a list of devices names. The names correspond to device names defined in <devicePorts> section. “defaultOutputDevice”: device to be used by default when no policy rule applies --> <modules> <!-- Primary Audio HAL is now hosted in audio_module_config_primary --> <!-- A2DP Audio HAL --> <!-- #ifndef OPLUS_BUG_STABILITY dongchuancheng@MULTIMEDIA.AUDIOSERVER.FRAMEWORK, 2022/09/29, disalbe unused a2dp module for qcom split bluetooth arch. <module name="a2dp" halVersion="2.0"> <mixPorts> <mixPort name="a2dp input" role="sink"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> </mixPort> </mixPorts> <devicePorts> <devicePort tagName="BT A2DP In" type="AUDIO_DEVICE_IN_BLUETOOTH_A2DP" role="source"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> </devicePort> </devicePorts> <routes> <route type="mix" sink="a2dp input" sources="BT A2DP In"/> </routes> </module> #endif OPLUS_BUG_STABILITY --> <!-- #ifndef OPLUS_FEATURE_BTAUDIO_LHDC //Yuepeifeng@TECH.CONN.BT.A2DP.36081 2020/03/29 for support LHDC codec--> <module name="bluetooth_qti" halVersion="2.0"> <mixPorts> <mixPort name="a2dp_lhdc output" role="source" flags="AUDIO_OUTPUT_FLAG_SPATIALIZER"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> </mixPort> <!-- #ifndef OPLUS_FEATURE_BTAUDIO_AID //fred.li@MULTIMEDIA.AUDIOSERVER.POLICY.1723426 2021/06/22 add for Hearing AIDs Audio Ports--> <mixPort name="hearing aid output" role="source"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="24000 16000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/> </mixPort> <!--endif OPLUS_FEATURE_BTAUDIO_AID--> </mixPorts> <devicePorts> <!-- A2DP Audio Ports --> <devicePort tagName="BT A2DP Out" type="AUDIO_DEVICE_OUT_BLUETOOTH_A2DP" role="sink" encodedFormats="AUDIO_FORMAT_LHDC AUDIO_FORMAT_LHDC_LL"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> </devicePort> <devicePort tagName="BT A2DP Headphones" type="AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES" role="sink" encodedFormats="AUDIO_FORMAT_LHDC AUDIO_FORMAT_LHDC_LL"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> </devicePort> <devicePort tagName="BT A2DP Speaker" type="AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER" role="sink" encodedFormats="AUDIO_FORMAT_LHDC AUDIO_FORMAT_LHDC_LL"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_OUT_STEREO AUDIO_CHANNEL_OUT_MONO"/> </devicePort> <!-- #ifndef OPLUS_FEATURE_BTAUDIO_AID //fred.li@MULTIMEDIA.AUDIOSERVER.POLICY.1723426 2021/06/22 add for Hearing AIDs Audio Ports--> <devicePort tagName="BT Hearing Aid Out" type="AUDIO_DEVICE_OUT_HEARING_AID" role="sink"/> <!--endif OPLUS_FEATURE_BTAUDIO_AID--> </devicePorts> <routes> <route type="mix" sink="BT A2DP Out" sources="a2dp_lhdc output"/> <route type="mix" sink="BT A2DP Headphones" sources="a2dp_lhdc output"/> <route type="mix" sink="BT A2DP Speaker" sources="a2dp_lhdc output"/> <!-- #ifndef OPLUS_FEATURE_BTAUDIO_AID fred.li@MULTIMEDIA.AUDIOSERVER.POLICY.1723426 2021/06/22 add for Hearing AIDs Audio Ports--> <route type="mix" sink="BT Hearing Aid Out" sources="hearing aid output"/> <!--endif /* OPLUS_FEATURE_BTAUDIO_AID */ --> </routes> </module> <!--endif OPLUS_FEATURE_BTAUDIO_LHDC--> <!-- Usb Audio HAL --> <module name="usb" halVersion="2.0"> <mixPorts> <mixPort name="usb_accessory output" role="source"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/> </mixPort> </mixPorts> <devicePorts> <devicePort tagName="USB Host Out" type="AUDIO_DEVICE_OUT_USB_ACCESSORY" role="sink"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/> </devicePort> </devicePorts> <routes> <route type="mix" sink="USB Host Out" sources="usb_accessory output"/> </routes> </module> <!-- #ifdef OPLUS_FEATURE_BTAUDIO_BINAURAL_RECORD --> <module name="binaural_record" halVersion="2.0"> <mixPorts> <mixPort name="Bluetooth record" role="sink"> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> </mixPort> </mixPorts> <devicePorts> <devicePort tagName="BT Record in" type="AUDIO_DEVICE_IN_PROXY" role="source" address=""> <profile name="" format="AUDIO_FORMAT_PCM_16_BIT" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> <profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED" samplingRates="44100 48000 96000" channelMasks="AUDIO_CHANNEL_IN_MONO AUDIO_CHANNEL_IN_STEREO"/> </devicePort> </devicePorts> <routes> <route type="mix" sink="Bluetooth record" sources="BT Record in"/> </routes> </module> <!-- #endif OPLUS_FEATURE_BTAUDIO_BINAURAL_RECORD --> <!-- #ifdef OPLUS_FEATURE_VIRTUAL_AUDIO --> <!-- ZhangHui@MULTIMEDIA.AUDIOSERVER.BASE, 2021/10/11, Add for virtual audio --> <xi:include href="/odm/etc/virtual_audio_policy_configuration.xml"> <xi:fallback></xi:fallback> </xi:include> <!-- #endif OPLUS_FEATURE_VIRTUAL_AUDIO --> <!-- Remote Submix Audio HAL --> <xi:include href="/vendor/etc/r_submix_audio_policy_configuration.xml"/> <!-- Bluetooth Audio HAL for hearing aid --> <!-- #ifndef OPLUS_BUG_STABILITY dongchuancheng@MULTIMEDIA.AUDIOSERVER.FRAMEWORK, 2022/09/20, disable unused module <xi:include href="/vendor/etc/bluetooth_qti_hearing_aid_audio_policy_configuration.xml"/> #endif /* OPLUS_BUG_STABILITY */ --> </modules> <!-- End of Modules section --> <!-- Volume section --> <!-- #ifndef OPLUS_BUG_STABILITY YeWenliang@MULTIMEDIA.AUDIODRIVER.HAL 2022/09/29 <xi:include href="/vendor/etc/audio_policy_volumes.xml"/> <xi:include href="/vendor/etc/default_volume_tables.xml"/> #else /* OPLUS_BUG_STABILITY */--> <xi:include href="audio_policy_volumes.xml"/> <xi:include href="default_volume_tables.xml"/> <!-- #endif /* OPLUS_BUG_STABILITY */ --> <!-- End of Volume section --> </audioPolicyConfiguration> 以上文件路径:/odm/etc/audio/audio_policy_configuration.xml 一加13 Coloros15,安卓版本15,修改该文件,绕过安卓音频SRC实现系统全局自适应采样率(Dynamic sampling rates)、自适应位深(Bit_perfect)(保持音频文件原始位深,16bit文件不升级位深)、禁用升频和重采样,关闭所有音频音效(effect)以及影响音频质量无损输出的一切音频处理,最大程度上抑制并降低音频抖动(jitter)(最大化优化时钟管理和同步)、最大程度抑制并降低音频失真和噪声以及电源纹波和噪声,以输出和输入最干净无污染最高质量的HIFI无损原始音频信号直出,并且输出修改过的完整文件(不要片段)
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值