1 Software Practice 3 Today s lecture Today s Task Porting Android App. in real device Prof. Hwansoo Han T.A. Jeonghwan Park 43
INTENT 2
3 Android Intent An abstract description of a message Request actions from other components start Activity start Service (after midterm) submit broadcast message Implicit / explicit Intent
4 Implicit Intent Create unspecified activity using pre-defined requests ACTION_VIEW webpage or map, etc ACTION_CALL phone call ACTION_SEND send email Activity B Activity A Intent 1 Intent-filter Intent 2 Intent 2 Activity C
5 Multiple Candidates for Intent Activity A Intent 1 Intent-filter Intent 1 Activity B Intent-filter Intent 1 Activity C
6 Example of Creating Implicit Intent EditText Button
Example of Creating Implicit Intent Constructor public Intent(String action) public Intent(String action, Uri uri) Action strings for instantiating Intent is statically defined in Intent class protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main); } Button button = findviewbyid(r.id.submit); button.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view view) { EditText edittext = MyActivity.this.findViewById(R.id.editText); Uri url = Uri.parse(editText.getText().toString()); Intent intent = new Intent(Intent.ACTION_VIEW, url); if (intent.resolveactivity(getpackagemanager())!= null) { startactivity(intent); } } }); 7
Example of Creating Implicit Intent Useful methods in Intent class as implicit public ComponentName resolveactivity(packagemanager pm); This method returns the name of component which can handle the given Intent protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main); } Button button = findviewbyid(r.id.submit); button.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view view) { EditText edittext = MyActivity.this.findViewById(R.id.editText); Uri url = Uri.parse(editText.getText().toString()); Intent intent = new Intent(Intent.ACTION_VIEW, url); if (intent.resolveactivity(getpackagemanager())!= null) { startactivity(intent); } } }); 8
9 Receiving Implicit Intent Receive with BroadcastReceiver Receive Intent which is broadcasted from application Usually used when application does not need to react the message Receive with Activity Receive Intent which is passed with startacitivty() method Used when Intent must be reacted via Activity
10 Explicit Intent Start An Activity specified in Intent object Pass parameter for personalizing Components Activity A Intent-filter Intent 1 (for B) Activity B Intent-filter Activity C
11 Why Need Explicit Intent id: pw: John ***** Login Hi,?
12 Why Need Explicit Intent id: pw: John ***** Hi, John! Login Explicit Intent
Example of Creating Explicit Intent Same class, but different constructor public Intent(Context packagecontext, class<?> cls); packagecontext is the Context of current Activity cls is the class of taget Activity Set parameter to Intent putextra(string name, ); // means various type protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.main); } Button button = findviewbyid(r.id.submit); button.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view view) { EditText edittext = MyActivity.this.findViewById(R.id.editText); Intent intent = new Intent(MyActivity.this, otheractivity.class); intent.putextra("mydata", edittext.gettext().tostring()); startactivity(intent); } }); 13
14 Receiver Activity of Implicit/Explicit Intent public Intent getintent(); Get Intent object which is submitted from previous Component Useful method in Intent class public String getstringextra(string name); returns the String data which declared as name if the type of return value is integer, use getintextra(name) protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.other); Intent intent = getintent(); String text = intent.getstringextra("mydata"); TextView textview = findviewbyid(r.id.textview); textview.settext(text);
15 Go Back to Previous Acitivity Android supports go-back mechanism as Back Stack Back Stack stores the history of previous Activitys startactivity(intent) method automatically set the current Activity to be paused and push current Activity into Back Stack
BROADCAST 16
17 Broadcast Sending message protocol which declared by Android system or other application Implicit Intent is exactly same with the broadcast message
18 Intent-filter Define the actions which Component will receive in AndroidManifest define a Component to receive ACTION_MAIN <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> define a Component to receive ACTION_VIEW <intent-filter> <action android:name="android.intent.action.view" /> </intent-filter> define a Component to receive user-defined action <intent-filter> <action android:name="this.is.my.own.action.hello_world" /> </intent-filter>
19 Broadcast Intent void sendbroadcast(intent intent); submit intent as broadcast message to Android system protected void oncreate(@nullable Bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Uri uri = Uri.parse("http://www.naver.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); } sendbroadcast(intent);
BroadcastReceiver BroadcastReceiver class is already implemented for you public class Receiver extends BroadcastReceiver { @Override public void onreceive(context context, Intent intent) { String action = intent.getaction(); } } if (action.equals("android.intent.action.view")) { Toast.makeText(context, intent.getdata().tostring(), Toast.LENGTH_LONG).show(); } *Toast: Android popup message 20
21 Manifest for BroadcastReceiver Define intent-filter <receiver android:name=".receiver"> <intent-filter> <action android:name="android.intent.action.view" /> <action android:name="android.intent.action.send" /> </intent-filter> </receiver>
22 [Lab Practice #4] Implement two applications satisfying the following conditions first application having two Activitys main Activity for generating explicit Intent to another Activity another Activity for generating implicit Intent other for receiving an Activity that cannot created from launcher, but only by Intent
23 [Lab Practice #4] main Activity of first application contain two EditText for getting name and keyword contain one Button for generating explicit Intent to another Activity another Activity of first application contain two TextView; name and keryword which is passed from explicit Intent contain a button for generating implicit Intent with ACTION_VIEW
24 [Lab Practice #4] Example of first application
25 [Lab Practice #4] main Acitivty of other application contain one TextView which prints the Uri from implicit Intent
26 [Lab Practice #4] Example of other application