Forum Discussion

Pataj's avatar
Pataj
New Contributor
10 years ago

Close android application with jscript

Hi,   How to close android application via a script written in jscript without use the screen ?   Thank you  
  • MarekFric's avatar
    10 years ago

    Hi,

     

    I use following function to restart native android app. If you need to just close android app then do not start app using last command "TestedApps.XXXXXXX.Run()"

     

    function restartAndroidApp(device)

    {
    var PackManagerObj = device.PackageManager;
    var Num = PackManagerObj.InstalledPackageCount;
    var PackObj

    for (var i = 0; i < Num; i++)
    {
    PackObj = PackManagerObj.InstalledPackage(Num - i - 1);
    Log.Message("Package number = " + i + ". Package name = " + PackObj.Name)
    if (PackObj.Name == "com.XXXXXX .... !!!!! this must be replaced with your package name !!!!!!!!!"){
    break
    }
    }

    try{
    while (device.PackageManager.IsActivePackage(PackObj)) {
    device.ShellExecute("am force-stop com.XXXXXXX")
    }
    } catch (e) {
    Log.Error("Following error occured during closing application: " + e)
    }

    TestedApps.XXXXXXX.Run()
    }

     

     

     

     

    Test script(example):

     

    //USEUNIT <nameOfScriptWithCommonFunctionsWhereFunction_restartAndroidApp_isPlaced>

     

    function ExampleTest123()
    {
    var deviceEnum = new Enumerator(Mobile)

    if (deviceEnum.atEnd()) {
    Log.Error("No connected device !!!")
    }
    while (!deviceEnum.atEnd())
    {
    var device = deviceEnum.item();

     

     

    // Setting the current device
    Mobile.SetCurrent(device.DeviceName, device.Index)

     

    //restart android app

    restartAndroidApp(device)

     

    //move to next connected device

    deviceEnum.moveNext();

     

    }