• Xamarin.Android 踩坑记


    将数据发送给微信

    var dbFile = Path.Combine(DBSetting.GetSetting().DBDirectory, $"{BLL.SelectProject.DBName}.db");
                            var sharefile = new Xamarin.Essentials.ShareFile(dbFile)
                            {
                                ContentType = "application/db"
                            };
                            await Share.RequestAsync(new ShareFileRequest
                            {
                                Title = $"分享项目文件{BLL.SelectProject.DBName}.db",
                                File = sharefile
                            });

    接收微信分享的文件

    在MainActivity.cs中

      //参考:https://blog.csdn.net/j5856004/article/details/102651886
        //参考:https://www.jianshu.com/p/2f2ffb6ec4bb
        //必须要单独写这个IntentFilter,否则桌面就没有图标了!!!!!重点中的重点
        [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher })]
        [IntentFilter(
            new[] { Intent.ActionView, Intent.ActionSend },// Intent.ActionView ,确保微信点击【使用第三方应用打开】时可以看到图标
            Categories = new[] { Intent.CategoryDefault },
            /*DataScheme = "file",*/ //如果QQ也要打开,这里要设置file
                                     //DataMimeType确保特定的文件,可以使用此APP打开
            /*DataMimeType = "application/octet-stream"*/ //设置了application/octet-stream,就可以接.db.octet-stream文件
            DataMimeType = "text/comma-separated-values" //*/*表示接收所有文件
            )]
        //这个是项目默认的,不用改
        [Activity(Label = "FmosMobile", Icon = "@mipmap/fmos", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
    
         }

     在MainActivity的OnResume方法重写中,加入:

       protected override void OnResume()
       {
                base.OnResume();
                this.SaveFile(Intent);
       }
    
     public static PYLMath.CommonExecuteResult<string> SaveFile(this Activity content, Intent Intent)
            {
                var res = new PYLMath.CommonExecuteResult<string>();
                //使用微信第三方应用打开文件时
                var extras = Intent.Extras;
                if (extras == null)
                {
                    res.IsSuccess = false;
                    return res;
                }
                string tempPath;
                try
                {
                    tempPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "temp");
                    if (!System.IO.Directory.Exists(tempPath))
                    {
                        System.IO.Directory.CreateDirectory(tempPath);
                    }
                }
                catch (Exception ex)
                {
                    res.InnerException = ex;
                    return res;
                }
                //
                string action = Intent.Action; //如果从微信中点击【使用第三方应用打开】,就是ActionView
                                               //如果微信分享的是*.db,那么 "application/db" ;如果是*.db..octet-stream,那么就application/octet-stream
                string type = Intent.Type;
                var uri = Intent.Data;
                int readCount = 0;
                char[] buffer = null;
                //创建一个请求
                try
                {
                    //参考:https://segmentfault.com/a/1190000021357383,关键是这几行!!!!!!
                    ParcelFileDescriptor parcelFileDescriptor = content.ContentResolver.OpenFileDescriptor(Intent.Data, "r");
                    var reader = new Java.IO.FileReader(parcelFileDescriptor.FileDescriptor);
                    var size = parcelFileDescriptor.StatSize;
                    if (reader.Ready() == false)
                    {
                        return res;
                    }
                    buffer = new char[size];
                    readCount = reader.Read(buffer, 0, (int)size);
                    //
                    parcelFileDescriptor.Close();
                    reader.Close();
                }
                catch (Java.IO.FileNotFoundException e)
                {
                    res.InnerException = e;
                    res.IsSuccess = false;
                    return res;
                }
                catch (Exception ex)
                {
                    res.InnerException = ex;
                    res.IsSuccess = false;
                    return res;
                }
                //
                if (readCount <= 0)
                {
                    return res;
                } 
                byte[] bytes = new byte[readCount];
                for (int i = 0; i < readCount; i++)
                {
                    bytes[i] = (byte)buffer[i];
                }
                try
                {
                    //
                    var fileName = System.IO.Path.GetFileName(Nancy.Helpers.HttpUtility.UrlDecode(Intent.DataString, System.Text.Encoding.UTF8));
                    var saveFile = System.IO.Path.Combine(tempPath, fileName);
                    System.IO.File.WriteAllBytes(saveFile, bytes);
                    //
                    res.Content = saveFile;
                    res.IsSuccess = true;
                    return res;
                }
                catch (Exception ex)
                {
                    res.InnerException = ex;
                    return res;
                }
            }

     打包的一些注意事项

    一、去掉钩

     二、存档

     

     

     重点:这个一定要保存好,所谓【证书/签名/密钥】等等,就是这个东西。

    如果每次不是用同一个【证书】,那么,在更新的时候,就会出现”证书不一致“,导致应用必须要卸载才能更新,最终后果就是,APP本地数据全部丢失!!!!

    还有,com.xxxxx.xxx.apk,中间的xxxxx.xxx一定要写好,在项目属性那里设置,一旦发布了被客户使用了,就没得后悔了,不然又得卸了重装,不能通过升级解决!!!

     

    保存为APK

     

     其他小坑: 

    一、

     切换成release才可以分发

    二、为了避免安装的时候,手机显示没有证书,最好给客户这个apk。(反正我试过没有什么问题,包括安装、升级啥的,或许不这样也行)

     

     

    三、不要乱升级Xamarin.Form,默认是5.0.0.2012版,否则会出现javac.exe错误,无解!!!!

    四、遇到调试时闪退,不报任何错误,不要打勾

    五、请在运行应用程序之前选择有效的设备 。

    (一)重装ADB:Win10 配置安装ADB教程总结20200514 - 知乎 (zhihu.com)

    六、XABLD7000: Xamarin.Tools.Zip.ZipException: Renaming temporary file failed: Permission denied

     (一)切换成release模式

     (二)不要打勾

     (三)重新生成、部署

     (四)切换回DEBUG模式

    七:使用Jar包:绑定 .JAR - Xamarin | Microsoft Docs

    八:使用aar包:用解压工具加压后,得到jar包

    九:一些很难用的jar包,使用VsCode,再用java包装一下导出jar包用。

     

    导出的问题:xamarin Unsupported class file major version 61

    解决方案:修改编译使用的JDK版本

    原本是17的,改成11。

     

    十:出现GSON错误,在管理NUGET包中,添加GoogleGson包。

    远程调试 

    在安装了ADB.exe的情况下,连上USB线

    调试的时候,选择

     

    使用ADB安装apk包。(注意只能连一台设备)

  • 相关阅读:
    k8s的快速使用手册
    prometheus
    k8s的存储卷
    nfs共享文件服务搭建
    k8s的基本使用
    Linux shell if [ -n ] 正确使用方法
    CURL使用方法详解
    LINUX下NFS系统的安装配置
    RedHat 6.2 Linux修改yum源免费使用CentOS源
    css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?
  • 原文地址:https://www.cnblogs.com/pylblog/p/15802525.html
Copyright © 2020-2023  润新知