-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathactivitystarter.html
More file actions
803 lines (803 loc) · 25.8 KB
/
Copy pathactivitystarter.html
File metadata and controls
803 lines (803 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="App Inventor for Android" name="description" />
<meta content="Android, Blocks App Inventor, Mobile, Phone, IDE" name="keywords" />
<title>
Activity Starter - App Inventor for Android
</title>
<link href="/static/images/appinventor-16.png" rel="SHORTCUT ICON" type="image/ico" />
<link href="/static/images/appinventor-16.png" rel="icon" type="image/png" />
<link href="/static/css/appinventor.css" rel="stylesheet" />
<script src="http://www.google.com/js/gweb/analytics/autotrack.js">
</script>
<script>
var tracker = new gweb.analytics.AutoTrack({
profile: 'UA-5856106-2'
});
</script>
<script language = "JavaScript" src="/static/scripts/HeaderAndFooter.js"></script>
<style>
img.c2 {border-width:0}
div.c1 {clear:both;}
</style>
</head>
<body>
<script language = "JavaScript">createHeader('<div id="navigation-breadcrumb"> <a href="/learn/">Learn</a> > <a href="/learn/reference/">Reference</a> > Activity Starter > ');</script><br>
<div id="aiac">
<div class="main-container">
<div class="customhr customhr-gray">
</div>
<div class="content">
<div class="content-body">
<div class="learn-page">
<h1>
Using the Activity Starter Component
</h1>
<p>
The activity starter component lets you combine applications by having one
application start up other applications. Activity Starter is mostly for advanced
developers, but it's a key way to extend the App Inventor, because it means lets
you take advantage of applications written by others, and created with other
development frameworks.
</p>
<p>
The apps you start can be App Inventor apps created by you or others; apps like
Camera and Maps that are pre-installed on the device; or any app at all, created
using App Inventor or not, as long as you have the necessary information to supply
to the activity starter.
</p>
<p>
For applications that are appropriately designed, you can pass values to the
applications when starting it. You can also start an application and get back a result
from it to use in further processing, provided that the application is designed to
return a result. Presently, you can pass and receive text values only.
</p>
<p>
To start another application, you must supply certain control information to the
Android operating system. You do this by setting various properties of Activity
Starter before calling the
<code>
StartActivity
</code>
method. This section gives
some examples.
</p>
<h2>
Starting Another App Inventor Application from your App Inventor App
</h2>
<p>
You can start another app that was created with App Inventor if you know its
package name and class name. If you have the source code of the app, you can find
these names as follows:
</p>
<ol>
<li>
Download the source code to your computer.
</li>
<li>
Using a file explorer or unzip utility, find the file
<code>
youngandroidproject/project.properties
</code>
.
</li>
<li>
The first line will start with "
<code>
main=
</code>
". Everything after that is
the package and class name.
</li>
</ol>
<p>
For example, here is a first line you might see for an App named HelloPurr.:
</p>
<p>
<code>
main=appinventor.ai_HomerSimpson.HelloPurr
</code>
</p>
<p>
The
<code>
HomerSimpson
</code>
part of the name comes from the App Inventor
user account.
</p>
<p>
To start this app, you would use an activity starter component with these
properties:
</p>
<code>
ActivityPackage: appinventor.ai_HomerSimpson.HelloPurr
</code>
<br />
<code>
ActivityClass: appinventor.ai_HomerSimpson.HelloPurr.Screen1
</code>
<p>
Invoking the activity starter's
<code>
StartActivity
</code>
method will start
HelloPurr. When HelloPurr finishes (if it does), the original app's
<code>
ActivityStarter.AfterActivity
</code>
method will be invoked.
</p>
<p>
<em>
Note:
</em>
If you are going to start another App Inventor app, make sure you
use the correct package name. For example, if someone posts the source code (zip
file) for an app, and you repackage that app, you'll end up with a different
package name than he had. In cases of doubt, or if you don't have the source code,
start the activity by hand on the phone and run
"adb logcat" to view the Android system log and see what was actually started, as
explained below in "Figuring out how to set the properties".
</p>
<em>
Note:
</em>
If you will be starting a second App Inventor app that you yourself are
writing, it's generally easier and more convenient to design the whole thing as a
single multiple screen app, rather than creating two separate apps.
See the section below on "Activity Starter versus Multiple Screen Apps".
<h2>
Starting a Builtin Phone Application from your App Inventor App
</h2>
<p>
Apps that come with the phone can be invoked using package names and class names, as
above. Android apps can also launch responses to an
<em>
intent
</em>
, which is the
Android operating system's way of specifying something to do without necessarily
designating a particular app. You can find information about intents (written for
advanced developers) in the
<a href="http://developer.android.com/guide/topics/intents/intents-filters.html">
Android
system documentation
</a>
.
</p>
<p>
Some apps are designed to take extra information when
they are launched. For example, maps can
take geographic information that specifies which map to display. Another example is
launching a Web search with extra information that specifies the text to search
for. You must consult the documentation for the particular app to learn what this
extra information is and how to specify it. Generally, you specify the information
by setting the activity starter's properties just before the other app is launched.
</p>
<p>
Here are some examples:
</p>
<h3>
Start the Camera
</h3>
<p>
To launch the Android Camera app, use an activity starter with these properties:
</p>
<p>
<code>
Action: android.intent.action.MAIN
</code>
<br />
<code>
ActivityPackage: com.google.android.camera
</code>
<br />
<code>
ActivityClass: com.android.camera.Camera
</code>
</p>
<p>
This is pretty much what the App Inventor Camera component does, although it is
much more convenient to use the Camera component.
</p>
<h3>
Launch a Web search
</h3>
<p>
To launch a Web search for a particular query, such as "Homer Simpson", use an
activity starter with these properties:
</p>
<code>
Action: android.intent.action.WEB_SEARCH
</code>
<br />
<code>
ExtraKey: query
</code>
<br />
<code>
ExtraValue: Homer Simpson
</code>
<br />
<code>
ActivityPackage: com.google.android.providers.enhancedgooglesearch
</code>
<br />
<code>
ActivityClass:
com.google.android.providers.enhancedgooglesearch.Launcher
</code>
<h3>
Open the browser to a Web page
</h3>
Use these activity starter properties to open a specific web page:
<p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri:
<a href="http://news.google.com">
http://news.google.com
</a>
</code>
</p>
<h3>
Start the mailer with pre-addressed message
</h3>
<p>
To start the Android mailer application, use the action
<code>
android.intent.action.VIEW
</code>
. You can use Activity starter's
<code>
DataUri
</code>
property to specify the recipient, the subject, and the
body of the message. In each case, starting the activity will bring up the Android
mailer, and you can complete the message and then press "Send".
</p>
<p>
For example, if you specify:
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri: mailto:santa@northpole.com
</code>
<p>
then starting the activity will bring up the mailer, with the addressee filed in.
</p>
<p>
If you specify:
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri: mailto:santa@northpole.com?subject=Please Santa&body;=Bring me a
pony
</code>
<p>
then starting the activity will bring up the mailer, with the addressee, the
subject, and the body filled in. You can use the mailer to edit these before
sending the message, if you prefer.
</p>
<p>
The DataURI here must be a URL encoded message designed to work with the internet
mailto service. Search the Web to find appropriate documentation and tools for
creating these, for example
<a href="http://email.about.com/od/mailtoemaillinks/a/mailto_elements.htm">
Elements of a Mailto: URL
</a>
</p>
<h3>
Show a map for a location
</h3>
<p>
If you know the latitude and a longitude of a location, you can show a map using an
activity starter with these properties to show a map of the area:
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri: geo:37.8,-122.23?z=23
</code>
<p>
The format of the DataURI is specific to the app. In this example, the URI
specifies a z (zoom) of 23, which is the largest zoom value. Zoom value is optional
and ranges from 1 (the entire Earth) to 23.
</p>
<p>
If you know the zip code, you can set the activity starter properties as follows:
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri: geo:0,0&q=94043
</code>
<p>
If you have a street address, you can use a DataUri that encodes the address with a
scheme called
<a href="http://www.w3schools.com/TAGS/ref_urlencode.asp">
URL
encoding
</a>
:
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
DataUri: geo:0,0&q=5000% 20MacArthurBlvd%20Oakland%2CCA
</code>
<p>
Generally in URI encoding the only characters you have to replace are spaces
(
<code>
%20
</code>
) and punctuation marks, such as comma (
<code>
%2C
</code>
) and
period (
<code>
%2E
</code>
).
</p>
<h3>
Play a YouTube video
</h3>
<p>
You'll need to know the URI of the YouTube video. Then set the Activity Starter
properties as
</p>
<code>
Action: android.intent.action.VIEW
</code>
<br />
<code>
ActivityPackage: com.google.android.youtube
</code>
<br />
<code>
ActivityClass: com.google.android.youtube.PlayerActivity
</code>
<p>
and set the Data URI to the URI of the video to be played, for example:
</p>
<p>
<code>
DataUri: http://www.youtube.com/watch?v=8ADwPLSFeY8
</code>
</p>
<h3>
Figuring out how to set the properties
</h3>
<p>
If you can't find documentation for the activities you want to start, one way to
figure out how to set the properties is to start up the activity manually and look
at what appears in the Android System Log. For example, if you use YouTube to play
a video, you'll see in the log:
</p>
<p>
<code>
I/ActivityManager( 86): Starting activity: Intent {
act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE]
dat=http://www.youtube.com/watch?v=8ADwPLSFeY8 flg=0x3800000
cmp=com.google.android.youtube/.PlayerActivity }
</code>
</p>
<p>
If you can find the "cmp=" string, then the ActivityPackage is the part before the
slash, and the ActivityClass is is the entire "cmp=" part, without the slash
character. As in the YouTube example, there may also be "dat=" information that can
specify with the DataUri property.
</p>
<h2>
Starting arbitrary apps
</h2>
<p>
You can use the Activity Starter to start any activity at all if you know the
package name and class name, or the appropriate intent. Some developers document
these intents for the benefit of other Android developers. For hints on starting
other apps using intents, see the
<a href="http://developer.android.com/reference/android/content/Intent.html">
Android API
documentation
</a>
or search the Android developer forums.
</p>
<p>
If you have an app on your phone and you don't have the source code, you might
still be able figure out the package name and class name (and sometimes the intent)
by launching the app and inspecting the Android system log, as indicated above.
</p>
<h2>
Receiving results from apps that you start
</h2>
<p>
Some apps are constructed to return a result. You can see that result as the
<code>
result
</code>
argument of the activity starter's
<code>
AfterActivity
</code>
event. Currently, App Inventor can receive text results only.
</p>
<h3>
Returning results from App Inventor apps, and getting the result
</h3>
<p>
You can create App Inventor apps that return a (text) result to their
callers, so they can be used as subroutines. To return a result, invoke the command
<code>
close screen with plain text
</code>
(located in the Control drawer). Your
subroutine app will terminate, and the argument of
<code>
close screen with
plain text
</code>
will become available to the app that called it.
If the calling app was itself an App Inventor app that used an activity starter,
it will see that text as
the argument to the activity starter's
<code>
AfterActivity
</code>
event.
</p>
<p>
In more detail, suppose there are two App Inventor apps:
A
<code>
SubroutineApp
</code>
designed to be
started with an activity starter and return a value, and a
<code>
CallerApp
</code>
that uses an ActivityStarter to call
<code>
SubroutineApp
</code>
. To return the
result,
<code>
SubroutineApp
</code>
executes
<code>
close screen with plain text
</code>
,
giving it as argument the result (text only) that should be returned to the caller.
</p>
<p>
On the caller side,
<code>
CallerApp
</code>
sets up its Activity Starter with the
correct package name and class name for starting
<code>
SubroutineApp
</code>
. It
must also set the
<code>
ActivityStarter.ResultName
</code>
property to the special
text string
<code>
APP_INVENTOR_RESULT
</code>
. Then
<code>
CallerApp
</code>
starts
the
<code>
SubroutineApp
</code>
activity. When the subroutine finishes, the Activity
Starter's
<code>
AfterActivity
</code>
event triggers,
and the result that was passed back from
<code>
SubroutineApp
</code>
will be available as the
<code>
result
</code>
argument to
<code>
AfterActivity
</code>
. That same information is available as the
ActivityStarter's
<code>
Result
</code>
property.
</p>
<h3>
Calling App Inventor Apps and passing values to them
</h3>
<p>
If you are writing an App Inventor
<code>
CallerApp
</code>
to call an
App Inventor
<code>
SubroutineApp
</code>
, you can pass a text value
to
<code>
SubroutineApp
</code>
.
To do this, use an Activity Starter in
<code>
CallerApp
</code>
with the
ExtraKey property to set to the special tag
<code>
APP_INVENTOR_START
</code>
and the ExtraValue
property set to the text you want to
pass. The
<code>
SubroutineApp
</code>
can retrieve that value by using
the
<code>
get start plain text
</code>
block from the Control drawer.
</p>
<p>
By passing and returning results, you can combine several App Inventor apps, both
apps you write, as well as apps you share with others.
</p>
<h3>
Returning results from arbitrary applications
</h3>
<p>
Getting values back from other applications is like getting values back from App
Inventor apps. In general in Android, an activity will
return a result that is designated by a
name, where the result is designated by a name, where the name is specified
with the Activity Starter's
<code>
ResultName
</code>
property. The name to use for
App Inventor apps is
<code>
APP_INVENTOR_RESULT
</code>
, as described above. Other
applications, not created with App Inventor, will use other names, and you'll need
to know those names in order get values back from
those applications. In general, you'll
need to consult documentation about the app you want to use as a subroutine.
Some developers
provide this information, or sometimes you'll have access to the source code.
</p>
<p>
Not all Android apps use the
<code>
Result
</code>
and
<code>
ResultName
</code>
mechanism. For example, some apps return information via the properties
<code>
ResultType
</code>
and
<code>
ResultUri
</code>
. Again, you'll need to have
information from the app developer to know which of these to use.
</p>
<p>
<em>
For advanced developers:
</em>
More specifically (with reference to the Android
developer documentation) an app can be designed to return an
<em>
intent
</em>
. The
Activity starter uses the specified
<code>
ResultName
</code>
to access
<code>
intent.getStringExtra(resultName)
</code>
to produce the result. The values
for
<code>
ResultType
</code>
and
<code>
ResultUri
</code>
come from
<code>
intent.getType()
</code>
and
<code>
intent.getType()
</code>
.
</p>
<h3>
Example: Picking files from the SD Card
</h3>
<p>
Here's an example that illustrates using a third-party application:
</p>
<p>
<a href="http://www.lysesoft.com/products/andexplorer">
AndExplorer from
Lysesoft
</a>
is an application that lets you pick files for your SD card. You can
call AndExplorer with an activity starter to add a file picking capability to your
application. You'll need to have AndExplorer installed on your phone. You can get
it from the Android Market.
</p>
<p>
To start AndExplorer to pick a file, use an ActivityStarter with:
</p>
<p>
<code>
Action: android.intent.action.PICK
</code>
<br />
<code>
dataType: vnd.android.cursor.dir/lysesoft.andexplorer.file
</code>
<br />
<code>
dataURI: file:///sdcard
</code>
</p>
<p>
When you start the activity and pick a file, the resulting file name will be
available as
<code>
ResultUri
</code>
. Also,
<code>
ResultType
</code>
will give the
type of the file, for example, image/jpeg or audio/mpeg. Using this, you can write
an app that lets you pick a file from the SD card and either displays the image or
plays the music track as appropriate.
</p>
<h3>
Is the application available?
</h3>
<p>
If your app calls other apps as activities, you'll want to check that those
other apps are available on your user's phone before trying to start them. You can
check this using the Activity Starter method
<code>
ResolveActivity
</code>
, which
returns the name of the Activity that would be started, given the package or intent
information you've provided. If this name is blank, then the required application is
not present, and you can warn your user.
</p>
<h3>
Using the Activity Starter versus using multiple screens
</h3>
<p>
The sections above explain how you can create an App Inventor app that calls
another App Inventor app, perhaps with passing and returning text values. If you
will be writing both apps, it's probably better to make a single
multiscreen app rather than create two apps and use the Activity Starter. This
will simplify things for your users, because they will need install only one app on
their device. Also, with multiple screen apps, you can pass and return lists as
values rather than just text, and you can share values
between the screens with TinyDB.
See the documentation on multiple screen apps for details.
</p>
<h4>
Note on upgrading old apps
</h4>
<p>
The activity starter commands described here have
changed from earlier releases, which used
<code>
close screen with result
</code>
and
<code>
get
start text
</code>
, rather than the newer
<code>
close screen
with plain text
</code>
and
<code>
get start plain
text
</code>
. If you load the source for an app from before 2012 and
release 66, App Inventor will flag the old blocks
and require you to replace them with the new versions.
</p>
</div>
</div>
</div>
<div class="footer">
<script language = "JavaScript">createFooter();</script><br>
<div class="footer-lastupdate">
<script>
if (document.lastModified != '') {
var m = "Page last updated: " + document.lastModified;
var p = m.length-8;
document.writeln("<center>");
document.write(m.substring(p, 0));
document.writeln("<\/center>");
}
</script>
</div>
</div>
</div>
</div>
</body>
</html>