Forum Xamarin.Forms

Cannot find StartActivity

Greetings,
I'm trying to update my app from our server whenver make a change.
But I am encountering a wall, as all the guide I find seems to ask me to use StartActivity, but I cannot find it in my app.

using Android.App;
using Android.Content;
using DeliveryService;
using Picking.Models;
using Plugin.SimpleAudioPlayer;
using SharpCifs.Smb;
using SharpCifs.Util.Sharpen;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.ServiceModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8080");

                    string path = @"smb://Domain/Share/IT/Androids/Picking.apk";
                    var auth = new NtlmPasswordAuthentication("Domain", "Username", "Pwd");

                    //Get target's SmbFile.
                    var file = new SmbFile(path, auth);



                    //Check if file exist
                    if (file.Exists())
                    {
                        Intent PromptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.Parse(file.GetPath()), "application/vnd.android.package-archive");

                        StartActivity(PromptInstall);

                    }
                    else
                    {
                        returnValue = false;
                    }

I get a red line below "StartActivity" simply stating that it does not exist in the current context. I included all the "using" in case it's just me forgetting one of them.

Best Answer

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @jb_delvaux said:
    I'm trying, from the common functions of the MVVM to download the latest version of the program from the server and start it as APK. Not much luck yet.

    That's not something that I've done. As that is a separate question from how to find StartActivity, I recommend starting a new forum thread for that specific question. It's more likely to be picked up by somebody who has done it or knows about it that way.

Answers

  • JohnHardmanJohnHardman GBUniversity admin
    edited January 14

    @jb_delvaux

    StartActivity is a method on the Android.Content.Context . Your main Activity class inherits from this (a long way down the class hierarchy), so you can call StartActivity from any non-static method in your main Activity class.

    To get the Context object from elsewhere, see https://stackoverflow.com/questions/43279971/get-current-activity-xamarin-android

  • jb_delvauxjb_delvaux Member ✭✭

    @JohnHardman said:
    @jb_delvaux

    StartActivity is a method on the Android.Content.Context . Your main Activity class inherits from this (a long way down the class hierarchy), so you can call StartActivity from any non-static method in your main Activity class.

    To get the Context object from elsewhere, see (Can't post links yet)

    Thank you. It helped me realize what the issue was. My solution is made of two project. Most of the code is in the MVVM part, which isn't the android part. This might explain why I can't just use startActivity.

    Now I'm just wondering if I should create a function to start the activity in the other project, or if it's better to send the context to the MVVM.

  • jb_delvauxjb_delvaux Member ✭✭

    Here's the code now. This is what I have and I get an error from

    No Activity found to handle Intent { act=android.intent.action.VIEW dat=smb://Domain/Share/IT/Androids/Picking.apk typ=application/vnd.android.package-archive flg=0x80000 }01-14 22:12:28.957 I/mono-stdout(29280): Android.Content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=smb://Domain/Share/IT/Androids/Picking.apk typ=application/vnd.android.package-archive flg=0x80000 }

    I didn't make the code for the app, I'm mostly maintaining it after someone left, so I'm not an expert in Xamarin form.
    The solution is made of two project, one MVVM and another that's mostly empty, being used only as a starting point.
    I'm trying, from the common functions of the MVVM to download the latest version of the program from the server and start it as APK. Not much luck yet.

    public async static Task<bool> UpdateToServerVersion()
            {
                bool returnValue = false;
    
                await Task.Run(() =>
                {
                    try
                    {
                        SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8080");
    
                        string path = @"smb://Domain/Share/IT/Androids/Picking.apk";
                        var auth = new NtlmPasswordAuthentication("Domain", "Username", "Pwd");
    
                        //Get target's SmbFile.
                        var file = new SmbFile(path, auth);
    
    
    
                        //Check if file exist
                        if (file.Exists())
                        {
                            Intent PromptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.Parse(file.GetPath()), "application/vnd.android.package-archive");
                            Android.App.Application.Context.StartActivity(PromptInstall);
    
                        }
                        else
                        {
                            returnValue = false;
                        }
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e.ToString());
                        returnValue = false;
                    }
                });
    
                return returnValue;
            }
    
    
  • JohnHardmanJohnHardman GBUniversity admin

    @jb_delvaux said:
    Now I'm just wondering if I should create a function to start the activity in the other project, or if it's better to send the context to the MVVM.

    Personally, I wouldn't pass the context to portable code. If you need portable code to call Android-specific code, you normally use a Dependency Service. See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @jb_delvaux said:
    I'm trying, from the common functions of the MVVM to download the latest version of the program from the server and start it as APK. Not much luck yet.

    That's not something that I've done. As that is a separate question from how to find StartActivity, I recommend starting a new forum thread for that specific question. It's more likely to be picked up by somebody who has done it or knows about it that way.

  • jb_delvauxjb_delvaux Member ✭✭

    @JohnHardman said:

    @jb_delvaux said:
    I'm trying, from the common functions of the MVVM to download the latest version of the program from the server and start it as APK. Not much luck yet.

    That's not something that I've done. As that is a separate question from how to find StartActivity, I recommend starting a new forum thread for that specific question. It's more likely to be picked up by somebody who has done it or knows about it that way.

    Thank you for all the advices ;-) I'll first look up your explanation on dependency service, see if I can figure it out myself, and if not I'll be back with another question.

Sign In or Register to comment.