如何在单声道下载apk后开始安装

本文关键字:apk 下载 开始 安装 声道 单声道 | 更新日期: 2023-09-27 18:19:55

我一直在努力从我的私人服务器上更新我的应用程序。我无法让广播接收器工作,也无法使用简单的安装脚本。我快疯了。如有任何帮助,我们将不胜感激。

我的脚本

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.Site.cc/Folder/app.apk");
            // If required by the server, set the credentials.
            //request.Credentials = CredentialCache.DefaultCredentials;
            //request.IfModifiedSince = DateTime.Parse("01-01-1990");
            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    //DateTime dt = System.IO.File.GetCreationTime(FilePath);        //.GetLastWriteTime(FilePath);
                   // DateTime appDate = DateTime.Parse(response.Headers["Last-Modified"].ToString());
                    try
                    {
                        ad = new AlertDialog.Builder(this).Create();
                        ad.SetTitle("INFO");
                        ad.SetMessage("Get New version");
                        ad.SetButton("NOW", delegate
                        {

                            System.IO.File.Delete("/filepath/app.apk");
                            string url = "http://www.Site.cc/Folder/App.apk";
                            DownloadManager.Request Request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
                            Request.SetDescription("Inspections Application");
                            Request.SetTitle("Download Inspections");
                            // in order for this if to run, you must use the android 3.2 to compile your app
                            if (Build.VERSION.SdkInt >= BuildVersionCodes.HoneycombMr1)
                            {
                                Request.AllowScanningByMediaScanner();
                                Request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                                Request.SetMimeType("application/vnd.android.package-archive");
                                Request.SetVisibleInDownloadsUi(true);
                            }
                            Request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, "Inspections.Inspections-Signed.apk");
                            // get download service and enqueue file
                            DownloadManager manager = (DownloadManager)GetSystemService(Context.DownloadService);
                            manager.Enqueue(Request);

                        });
                        ad.SetButton2("LATER", delegate
                        {
                            return;
                        });
                        //ad.SetButton("Continue",new IDialogInterfaceOnClickListener listner);
                        ad.SetCanceledOnTouchOutside(true);
                        ad.Show();

                    }
                    catch (System.Exception ex)
                    {
                        ad = new AlertDialog.Builder(this).Create();
                        ad.SetTitle("INFO");
                        ad.SetMessage(ex.Message);
                        ad.SetCanceledOnTouchOutside(true);
                        ad.Show();
                    }
                   /* 
                        cL.DownloadFile("http://www.site.cc/folder/app.apk", Path);
                        Intent promptInstall = new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse("/sdcard/Download/Inspections.Inspections-Signed.apk")).SetType("application/vnd.android.package-archive");
                        StartActivity(promptInstall); 
                    */
                    //string lastModified = DateTime.Parse(response.Headers["Last-Modified"].ToString()).ToString();
                }
            }
            catch (WebException wex)
            {
            }

如果我能在下载完后找到一种方法来启动应用程序的安装向导

我知道它必须请求权限

设备日志:

06-28 07:17:55.843 I/MonoDroid(  860): UNHANDLED EXCEPTION: Android.Content.ActivityNotFoundException: Exception of type 'Android.Content.ActivityNotFoundException' was thrown.
06-28 07:17:55.843 I/MonoDroid(  860): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.7-branch/0e9eea34/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:616
06-28 07:17:55.843 I/MonoDroid(  860): at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent) [0x0006b] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.7-branch/0e9eea34/source/monodroid/src/Mono.Android/platforms/android-12/src/generated/Android.Content.ContextWrapper.cs:2318
06-28 07:17:55.843 I/MonoDroid(  860): at Inspections.Activity1.<checkdate>b__8 (object,Android.Content.DialogClickEventArgs) [0x00031] in f:'aa'c#'android'Inspections'Activity1.cs:519
06-28 07:17:55.843 I/MonoDroid(  860): at Android.Content.IDialogInterfaceOnClickListenerInvoker.n_OnClick_Landroid_content_DialogInterface_I (intptr,intptr,intptr,int) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.7-branch/0e9eea34/source/monodroid/src/Mono.Android/platforms/android-12/src/generated/Android.Content.IDialogInterface.cs:172
06-28 07:17:55.843 I/MonoDroid(  860): at (wrapper dynamic-method) object.0ae0dbc0-1a91-4175-9a2f-597c804b25ec (intptr,intptr,intptr,int) <IL 0x00018, 0x0003b>
06-28 07:17:55.843 I/MonoDroid(  860): 
06-28 07:17:55.843 I/MonoDroid(  860):   --- End of managed exception stack trace ---
06-28 07:17:55.843 I/MonoDroid(  860): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive flg=0x10000000 }
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1580)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Activity.startActivityForResult(Activity.java:3390)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Activity.startActivityForResult(Activity.java:3351)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Activity.startActivity(Activity.java:3561)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.Activity.startActivity(Activity.java:3529)
06-28 07:17:55.843 I/MonoDroid(  860):  at mono.android.content.DialogInterface_OnClickListenerImplementor.n_onClick(Native Method)
06-28 07:17:55.843 I/MonoDroid(  860):  at mono.android.content.DialogInterface_OnClickListenerImplementor.onClick(DialogInterface_OnClickListenerImplementor.java:29)
06-28 07:17:55.843 I/MonoDroid(  860):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.os.Looper.loop(Looper.java:137)
06-28 07:17:55.843 I/MonoDroid(  860):  at android.app.ActivityThread.main(ActivityThread.java:4867)
06-28 07:17:55.843 I/MonoDroid(  860):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 07:17:55.843 I/MonoDroid(  860):  at java.lang.reflect.Method.invoke(Method.java:511)
06-28 07:17:55.843 I/MonoDroid(  860):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-28 07:17:55.843 I/MonoDroid(  860):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-28 07:17:55.843 I/MonoDroid(  860):  at dalvik.system.NativeStart.main(Native Method)
06-28 07:17:55.873 W/dalvikvm(  860): JNI WARNING: JNI method called with exception pending
06-28 07:17:55.873 W/dalvikvm(  860):              in Lmono/android/content/DialogInterface_OnClickListenerImplementor;.n_onClick:(Landroid/content/DialogInterface;I)V (CallObjectMethod)
06-28 07:17:55.873 W/dalvikvm(  860): Pending exception is:
06-28 07:17:55.873 I/dalvikvm(  860): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive flg=0x10000000 }
06-28 07:17:55.873 I/dalvikvm(  860):   (raw stack trace not found)
06-28 07:17:55.873 I/dalvikvm(  860): "main" prio=5 tid=1 NATIVE
06-28 07:17:55.873 I/dalvikvm(  860):   | group="main" sCount=0 dsCount=0 obj=0x4127a508 self=0x41269450
06-28 07:17:55.873 I/dalvikvm(  860):   | sysTid=860 nice=0 sched=0/0 cgrp=[no-cpu-subsys] handle=1074679600
06-28 07:17:55.873 I/dalvikvm(  860):   | schedstat=( 3794555681 1447662333 6173 ) utm=289 stm=90 core=0
06-28 07:17:55.883 I/dalvikvm(  860):   #00  pc 00001260  /system/lib/libcorkscrew.so (unwind_backtrace_thread+27)
06-28 07:17:55.883 I/dalvikvm(  860):   #01  pc 0005f97c  /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+35)
06-28 07:17:55.883 I/dalvikvm(  860):   #02  pc 00053824  /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+303)
06-28 07:17:55.883 I/dalvikvm(  860):   #03  pc 000538be  /system/lib/libdvm.so (dvmDumpThread(Thread*, bool)+25)
06-28 07:17:55.883 I/dalvikvm(  860):   #04  pc 00038e7a  /system/lib/libdvm.so
06-28 07:17:55.883 I/dalvikvm(  860):   #05  pc 00040fd8  /system/lib/libdvm.so
06-28 07:17:55.883 I/dalvikvm(  860):   at mono.android.content.DialogInterface_OnClickListenerImplementor.n_onClick(Native Method)
06-28 07:17:55.883 I/dalvikvm(  860):   at mono.android.content.DialogInterface_OnClickListenerImplementor.onClick(DialogInterface_OnClickListenerImplementor.java:29)
06-28 07:17:55.883 I/dalvikvm(  860):   at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
06-28 07:17:55.883 I/dalvikvm(  860):   at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 07:17:55.883 I/dalvikvm(  860):   at android.os.Looper.loop(Looper.java:137)
06-28 07:17:55.893 I/dalvikvm(  860):   at android.app.ActivityThread.main(ActivityThread.java:4867)
06-28 07:17:55.893 I/dalvikvm(  860):   at java.lang.reflect.Method.invokeNative(Native Method)
06-28 07:17:55.893 I/dalvikvm(  860):   at java.lang.reflect.Method.invoke(Method.java:511)
06-28 07:17:55.893 I/dalvikvm(  860):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-28 07:17:55.893 I/dalvikvm(  860):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-28 07:17:55.893 I/dalvikvm(  860):   at dalvik.system.NativeStart.main(Native Method)
06-28 07:17:55.893 I/dalvikvm(  860): 
06-28 07:17:55.893 E/dalvikvm(  860): VM aborting
06-28 07:17:55.893 I/mono    (  860): Stacktrace:
06-28 07:17:55.893 I/mono    (  860): 
06-28 07:17:55.893 I/mono    (  860):   at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr) [0x00000] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.7-branch/0e9eea34/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:129
06-28 07:17:55.893 I/mono    (  860):   at Java.Lang.Throwable.get_Message () [0x0003e] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.7-branch/0e9eea34/source/monodroid/src/Mono.Android/platforms/android-12/src/generated/Java.Lang.Throwable.cs:175
06-28 07:17:55.893 I/mono    (  860):   at (wrapper runtime-invoke) <Module>.runtime_invoke_object__this__ (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

如何在单声道下载apk后开始安装

您需要使用Intent参数启动一个活动,如下所示:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

apkUri是一个指向APK文件的URI。

好的,所以我找到了解决方案

System.IO.File.Delete("/sdcard/download/App_Name.apk"); //Delete previous version

cL.DownloadFile("http://www.Site.cc/Folder/App_Name.apk", "/sdcard/download/App_Name.apk");// Download new app via HttpWebRequest

新的工作代码

Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/download/" + "App_Name.apk")), "application/vnd.android.package-archive");
promptInstall.AddFlags(ActivityFlags.NewTask);
StartActivity(promptInstall);

旧的不工作代码

Intent promptInstall = new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse("/sdcard/Download/App_Name.apk")).SetType("application/vnd.android.package-archive");
promptInstall.AddFlags(ActivityFlags.NewTask);
StartActivity(promptInstall);

新手问题,但它解决了