Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to install apk programmatically via without publish ? Any body know answer let us know

$
0
0

Hi all,
I have code for manually download the url just open it browser but get error

1.First way
if (!string.IsNullOrEmpty(url))
{
Toast.MakeText(this, "APK URL:" + url, ToastLength.Long).Show();

                    Intent goToMarket = new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(url));
                     StartActivity(goToMarket);
                }

2.Second way

                if (!string.IsNullOrEmpty(url))
                {
                    Toast.MakeText(this, "APK URL:" + url, ToastLength.Long).Show();                        
                    Intent goToMarket = new Intent(Intent.ActionView);
                    Android.Net.Uri RIO = Android.Net.Uri.Parse(url);
                    goToMarket.SetDataAndType(RIO, "application/vnd.android.package-archive");
                    goToMarket.SetFlags(ActivityFlags.NewTask);
                    //goToMarket.AddFlags(ActivityFlags.NewTask);                         
                    StartActivity(goToMarket);
                }
                  I have attached the screen shot [2 names of file] second images

3.Third way
public void DownloadFile(string m_uri)
{
try
{
var webClient = new WebClient();

            webClient.DownloadFileCompleted += (s, e) =>
            {
                // string error = e.Error.Message;
               ** if (e.Error != null)
                {
                    Toast.MakeText(this, "Exception is occured in DownloadFile FileCompleted Method " + e.Error.Message.ToString(), ToastLength.Long).Show();
                    return;
                }**
                //var bytes = e.Result; // get the downloaded data
                // string documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/download/";
                string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString();
                /************* GetDate ************/
                DateTime appDate = DateTime.Now;
                DateTime Jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                /// <summary>Get extra long current timestamp</summary>
                long Millis = (long)((appDate - Jan1St1970).TotalMilliseconds);
                Millis = Millis - 7200000; //current Time
                long time = DateTime.Now.Ticks;
                /************** Declare Notification Manager *************/
                this._notificationManager = this.GetSystemService(NotificationService) as NotificationManager;
                if (this._notificationManager == null)
                {
                    throw new System.Exception("couldn't get a reference to the notification service");
                }
                /************* Set Notification ***************/
                // Set the icon, scrolling text and timestamp
                Notification notification = new Notification(Resource.Drawable.Icon, "Updates", Millis);
                notification.Flags = NotificationFlags.AutoCancel;
                notification.Flags = NotificationFlags.OngoingEvent;
                //notification.Number = 3;
                //Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/download/" + "FileName.apk")), "application/vnd.android.package-archive");
                Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString())), "application/vnd.android.package-archive");
                promptInstall.AddFlags(ActivityFlags.NewTask);
                // The PendingIntent to launch our activity if the user selects this notification
                PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, promptInstall, 0);
                var nBuilder = new Notification.Builder(this);
                // Set the info for the views that show in the notification panel.
                notification.SetLatestEventInfo(this, "Update Complete", "Click to Install", contentIntent);
                // Send the notification.
                // We use a layout id because it is a unique number.  We use it later to cancel.
                // TODO: switch to a unique identifier
                /************* Send Notification ************/
                this._notificationManager.Notify(666, notification);
                //System.IO.File.WriteAllBytes(m_filePath, bytes); // writes to local storage 
                StartActivity(promptInstall);
            };
            var url = new System.Uri(m_uri);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
            //webClient.DownloadFileAsync(url, Android.OS.Environment.ExternalStorageDirectory + "/download/com.xamarin.bluetooth.le.apk");
            webClient.DownloadFileAsync(url, Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString());
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
    {            
        int length = Convert.ToInt32(e.TotalBytesToReceive.ToString());
        int prog = Convert.ToInt32(e.BytesReceived.ToString());
        int perc = Convert.ToInt32(e.ProgressPercentage.ToString());
    }

I have attached the screen shot [3 names of file] third images

  1. Fourth way
    Using download manager class i just comment the following code after that i build code

               if (e.Error != null)
                {
                    Toast.MakeText(this, "Exception is occured in DownloadFile FileCompleted Method " + e.Error.Message.ToString(), 
                    ToastLength.Long).Show();
                    return;
                }
    

    I have attached the screen shot [4 names of file] foruth images

5.Paste the AndroidManifest.XML file









I have attached the screen shot [5 names of file] fith images for code build style


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>