How to Launch application via variable in droidscript?

375 views
Skip to first unread message

Peter Parker

unread,
Mar 10, 2019, 4:18:10 AM3/10/19
to DroidScript
Hi there!
I am creating a voice assistant application via Droidscript,

But i don't know how to launch applications via variable..
So that whenever i say "Open 'App name' " It will find appropriate matching for that application and open it.
(Sorry for bad english)

~Peter

nikhil babychan

unread,
Mar 10, 2019, 7:46:51 AM3/10/19
to DroidScript
For getting the app list you can use:
app.GetActivities();
And use indexOf() and find the app,
then to open the app:
var action = "android.intent.action.MAIN";
app.SendIntent( this.packageName, this.className, action );

(If you are a premium users, look at the Launcher sample, If not, I recommend to become premium)

Peter Parker

unread,
Mar 10, 2019, 12:15:03 PM3/10/19
to DroidScript
Hi Nikhil,

I tried this, but I was unable to figure out what IndexOf () statement does. ('Cause I'm new to javascript / droidscript) ...

Explanation will be more helpful with examples.

Thanks.

nikhil babychan

unread,
Mar 10, 2019, 12:27:29 PM3/10/19
to DroidScript
string.indexOf() is used to find match.
refer:
https://www.w3schools.com/jsref/jsref_indexof.asp

Steve Garman

unread,
Mar 10, 2019, 2:49:47 PM3/10/19
to DroidScript
@Peter

I am not sure that Nikhil's suggestion will give you enough information to use the activities list. Personally for your purposes I would build some sort of index

The code below is not entirely aimed at a beginner either but the buildIndex function may be useful to you.

//
var activities;

function OnStart()
{
lay = app.CreateLayout("Linear", "VCenter,FillXY");

edt = app.CreateTextEdit("Gmail", -1, -1, "singleLine");
edt.SetOnEnter(openApp);
lay.AddChild(edt);

app.AddLayout(lay);

activities = buildIndex(app.GetActivities());
edt.Focus();
app.ShowKeyboard(edt);
}

function openApp()
{
var label = this.GetText().toLowerCase();
var rec = activities[label]
var action = "android.intent.action.MAIN";
if(rec)
{
app.SendIntent(rec.packageName,
rec.className, action);
}
else
{
alert(label + " Not Installed")
}
}

function buildIndex(obj)
{
var len = obj.length;
var rec, newRec, ret = {};
for(var i = 0; i < len; i++)
{
rec = obj[i];
ret[rec.label.toLowerCase()] = {
packageName: rec.packageName,
className: rec.className };
}
return ret
}

Peter Parker

unread,
Mar 10, 2019, 11:12:06 PM3/10/19
to DroidScript
It worked!
Thanks a lot Steve.

Jared

unread,
Apr 2, 2019, 8:10:17 AM4/2/19
to DroidScript
Thanks Steve this is really cool!

I made a slight modified version that lists all installed and launches on touch. Here it is if anyone wants it :D


var activities;

function OnStart()
{
lay = app.CreateLayout("Linear", "VCenter,FillXY");

// edt = app.CreateTextEdit("Gmail", -1, -1, "singleLine");
//edt.SetOnEnter(openApp);
var pkgs = app.GetActivities();
lst = app.CreateList("",1,1,"");
app.ShowProgressBar("Loading...");
for (var i = 0; i < pkgs.length; i++)
{
app.UpdateProgressBar(( i / pkgs.length)*100)
lst.AddItem(pkgs[i].label, pkgs[i].className);
app.ShowPopup(i);
}
lst.SetOnTouch(function (title,body,image,index){
this.title = title;
this.body = body;
this.image = image;
this.index = index;
openApp(this.title)
});
app.HideProgressBar()
lay.AddChild(lst)
// lay.AddChild(edt);

app.AddLayout(lay);

activities = buildIndex(app.GetActivities());
// edt.Focus();
lst.Focus()
// app.ShowKeyboard(edt);
}

function openApp(item)
{
this.item = item
var label = this.item.toLowerCase();


var rec = activities[label]
var action = "android.intent.action.MAIN";
if(rec)
{
app.SendIntent(rec.packageName,
rec.className, action);
}
else
{
alert(label + " Not Installed")
}
}

function buildIndex(obj)
{
var len = obj.length;
var rec, newRec, ret = {};
for(var i = 0; i < len; i++)
{
rec = obj[i];
ret[rec.label.toLowerCase()] = {
packageName: rec.packageName,
className: rec.className };
}

return ret;
}

Reply all
Reply to author
Forward
0 new messages