You are on page 1of 39
BROADCASTS it Intents, Passing to Dial a Number (of) Tntent, Using Intents Data to Intents, Getti t0 Send SMS. Int) to communicate with a vy ‘also use Intent to support interaction among any of the Be Geisiied on an Android device, there Is no matter whlch applic Of this tums our device from a platform containing 2 collection of ind most common uses for Int ents into a single, interconnected system. The It can also use Intents to Broad Mipistart new Activities, either explicitly, or implicitly Messages across the system EERIE USING INTENTS TO LAUNCH ACTIVITIES ? The main use of Intents is to bin: P rm nd communicate This between them. Intents are used workflow | of different screens. wht eos display on — | an Intent, as follows, a StartActivity(myinter fi To The above method find wa our Intent. We can also const open means | knowing ‘or to include the an action t a Our application won't rec ctivity finishes when we are usin ou cl feedback from a sub-activity. tt GEMM EXPLICITLY STARTING NEW A you my add co To create a new Intent, we have tial the current activity’s context and the clas this Intent 5 into StattActivitly which is shown in the follc pee | lue Intent intent=new Intent (MyActivity h ! startActivity (intent); i | ‘After the StarAciiviy is called, then the new Activity (in the example, MyotherActviil will be created, started and resumed by moving the top of the activity stack. On the new Activity, by calling finish or pressing the’ hardware back button, it ¢ Temoves it from the stack. Alternatively we can continue to navigate to other acti g SiartActivity. Note that each time when we call StartActivity, a new activity) : calls from our app! tt jar implicit intent and that requests the (i.e. represented as URI). iingWired && itDontLookGooa) Intent intent=new PlanActivity inte } This intent, resolve ¢ — aleghina ity and provides the dial action ae hone nur When we constr ecity an acted Sl ib, Optionally, supply ‘m that action, We can also Send additional data t to the aa To start an acti oid will resolve it inte the ‘wdivity class and pe p f data that specified. This means we can create f ther applications without Imnowing the exactly v onality from ahead of time. EEMBPassiNG Data Tt is possible t using Intents. To do that We Must enhance th c Jata in the startintent and Bd code in Secondary putExtra() method Th MainActivity, one line alkingandroid. MESSAGE and the: Pisses @ key and a value. In t Male is “Hello Seco yActivity Intent startinter wl BecondaryActivity.class) q yActivity.c MESSAGE”, *HelloSecondaryActivity yi Blaflintent putextra(*com. talkingandrold. MESSAS PlaMtActivity(startintent) e this data. ecelv secondaryActivity to F also need to make changes to Seconddly! yee d sh tnge SecondaryActivity to receive the data ant at it has a property called id. In the design view, you ¢ message as the new id. In the text view, you will In the onCreate( ) method of SecondaryActivity, you get the passed Intent by e ithe getintent( ) method. After you have the intent, you can get the String that was p used in MainActivily, J By using the gotStingExtra( ) method with same key that was Program 5.5.1 give the of method on line 1 e getStringExtral ) method on line 2, The passed message is displayed by cal line 4 fone Fig. 5.5.1 sho a START SECOND lil PROFESSIONAL ge com ‘alkingandroid.hour2application; port android.content.intent Gimport android. support.v7 app. Actions: arActivity; 4:import android.os.Bundie S:import android. view Menu G:import android. view.M Tsimport android 8:impo! 1 23:stal 24:} 25:1); 26:} 27://menu 28:} code excluded co ‘TION DEVELOPMENT ort android. view Menue; G:import android. view. Menultem: T:import android.view.TextView, 8 @:public class SecondaryA 10:@ Override Viprotected 42:super.onCrea 14;setContentView(R. code 15:intent=getintent 16:String messa Intent, getStringExtra 17:TexiView mes findViewByla(R. id. gimessagoTextVi aa 20:// menu code excluded 21} B EEGHIN GETTING RESULTS FROM ACTIVITIES a If an activity which is started via StartActvity is independent of its parent and Wil _ not provide any feedback when it closes. Where feedback is required, then we can fan Activity as a sub-Activity that can pass results back to its parent. In different the sub-Activities are actually just activities opened. We must register them If ‘application manifest in the same way as any other Activity. Any manifest registered ‘can be opened as a sub-activity then it includes those provided by the system oF applications. code it gives the launch ing a sub. acti Blatic final int SHOW_suBACT SUBACTIVITY = Ipivate void startSubActivity; ) Intent ir startActiv } Regular activ code uses an impl ¥ or explicitly. The following k @ contact { ? EB Returning Resul When our ct Pay our sub-activi return, then cal before finish to retin oo ° the calling activity. T .ethod takes two parameters they are resill : lah! the result data itself, represented as an Intent. The result code is the "resuit® Tuning the sub-activity generally, eithe ty In some circumstances, where OK ay the available results, but we want t) SotResult supports any RESULT_OK or ACTIVITY.RESULT_CANCELEL EMielied do not sufficiently or accurately describe WP our own response codes to handle application specific choles value. rom a sub-activity’s onGreal is taken fr ults to the following Program 5.6.1 i in might be different res! an OK and cancel butto! eed Horse id - Miu gerselaorneeeuee Url selectedHorse = Uri-parse(‘content'//horses/” + selected new Intent-(Intent.ACTION.PICK, selectedHorse) Intent result = setResult (RESULT_OK i, finish( ) yi Button cancelButton = (Bu! f cancelBulton.setOnC public void onClick setResull(R finish( ): vn eee Returni ERGEB Handling Sub-Activity Results When a sub-activity closes, then the event handler is fired within lling Activity. Override this method to handle the results that rned by sub-at Following are the number of parameters that receives the OnActiveResult hal (1) Request Code : This was used to launch the returning sub-activity, (2) Result Code : This code set by the sub-activity to indicate its result. ctivity, it may include the URI and that represents a Wold OnActivityResult(int requestCode, int cd resultCode, Super. OnActivityResult(requestCod questCode, resultCode, data). Switch (requestCoc Actiity Rest Randle] ERMB Native Actions The following list s onstants in the Intent cl en we create imp Miey are known as Activity Int start activities and sub-activities within: GU iad ions that are available as statie String: ative acti y the launcher. It opens an ‘Activity and h ACTION ALL Apps : This is handled b Uists all the installed applications. ANSWER : This is handled by the native in-call screen. | that handles incoming calls. the number to dial pi ACTION DIAL : It brings dialer application with 7 the intent’s data URI. This is handled by the native Android phone 55 1212 are both valid nul Example : tel it the data at that intent an € ACTION_EDIT : It requ URI. (9) ACTION_INSERT : It specified in the Inte! tems into the Cui * (10) ACTION_PICK : It lau ‘em from tie content provider specif i (11) ACTION_SEARCH : It fired witholle a specific Activity, then plications thats support search (12) ACTION_SEARCH_LONG_PRESS de a shortolle to a voice search. On the > intercept longa presses 4 (13) ACTION_SENDTO : It launches it e contact specified the intent’s data UR (14) ACTION SEND : It launches an activity and that sends the data specified in the inl The recipient contact needs to selected by the resolved activity. Use Gna ACTION_SEND action to send data to a remote recipient (15) ACTION VIEW : In this, view asks that the data is supplied in the intent’s d be viewed in the most reasonable manner. Different applications will hi Tequest depending on the URI schema of the data supplied. | ACTION_WEB.SEARCH : It opens the browser and to perform a web | 9n the query supplied by using the SearchManago:. QUERY ice for initiating 's to use an Ini Phone calls { use umber to dial by setting the Intents data usin the ga ft Whoyoungonnacall = new Intent(| ACTION_DIAL, Uni.pansel ISlantActivity(whoyoungonnaca This starts a dialer Activity that st ipecified. The default tivity uld be prepopulated with the Initiating the call. As a re ¢ user to change the number Before Special permissions ntent action doesn't equine By using a decoupled from th er, your application stays! have installed a example, if users: number from your J Intents to dial @ ERIM Replacing tI Replacing t (i) Intercept inte 2) Initiate ding to a user or making ction. The native Pressing the hardwai @n ACTION_DIAL + | To intercept t the Ahanifest entries for your Feplacement dialer Ac 3 actions 1 the device's hardware (1) Intent.ACTION_CALL_BUTTON tic adcast whet fall button is pressed Intent Filter that listens for this scones Meiaily action, described in the previous section, is Hse (2) Intent.ACTION_DIAL : This Intent action, by applications that want to initi this action should be both default browser) and must specify the tel Sugh it can support additional schen ae The Intent Filter used tO ort dial requests from ate a phone call it and browsable (to SUPP schema to replace existing dlalet mes). ‘TION DEVELOPMENT ‘action android:name="android intent.action. CALL_BUTTO category android:name = ‘android intent.category DEFAULT” a a messageArray = smsManager.dividemessage(myMessage)} ArayListsentintents = new ArrayList( ) for (int i = 0; | < messageArray.size( ); i++) sentintents.add(sentP!); ‘smsManager.sendMultipartTextMessage(sendTo, null, messageArray, sentintents, null); Binary data via SMS using the sendDats ‘S much like send SendDataMessage method work. Parameters for the destination you want to send Biting sendTo="5551234 Short destinationPort=¢ bytel } data=| smsManager.s¢ YLLABUS [M.TECH - HYDERABAD] Resolving Intent Filters, Finding and Using Intents Using Intent Filters to Service Implicit Intents Received within an Activity. a appropriate action. Following are the two important steps that ‘system broadcast intents, to make BroadCastReciever (2) Creating the broadcast receiver. (2) Registering the broadcast receiver. There is an additional steps in case we are getting t t, our custom intents then it will have to create and broadcast those intent GRRE creating the Broadcast Receiver A broadcast receiver is implemented a: ube ceiver class and Fitod wh 2 Intent object overriding the onRec parameter. The following is an code shown public class MyReceiver ext Toast.makeText(context N ( 7 ERE MB Registering Broadcast Receiver ‘An application listens for specific broadcast intents by registering a broad in AndroidManifest.xml file, Consider we are going to register MyReceiver! tags. Following are the standard values or we can al that provided by Android, Iso specify our own & is action should be available as () ALTERNATIVE : This category specifies that thi ‘alternative to the default action performed on an Item of this datatype: f IVE ¢ ’ (i) SELECTED_ALTERNATIVE : It is very sim [ E category, but ii this category it will always resolve t he intent resolution: described next, this SECLEC t of possibilities! is required. BROWSABLE : It spec owser. Within the browser when ar ble category, (iv) DEFAULT : Set this he datatype specified in the Int launched using an explicit inten on 4 (v) HOME : By setting an i an action, thai then we are pres rail wit (vi) LAUNCHER : By usi application launcher. (3) data : This tag enabl can act Of We can inciude several data combination 6f the following attributes to specif pports () android:host : It specifies a vz 0 Example : google.com (ii) android:mimetype : It specifies which type of data our component is capable - handling. as Example : \e Maps location ectively rather nd path attributes ode. 6.1.41 from Android puts together a list of all the intent filters available fe ~ jmport android.app Activity import android. app.N import android.c rent.action. MAINS. > sandroid.int

You might also like