Mobile Programming Lecture 5

71
Mobile Programming Lecture 5 Composite Views, Activities, Intents and Filters

description

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters. Lecture 4 Review. How do you get the value of a string in the strings.xml file? What are the steps to populate a Spinner or ListView using XML? How many Android application components are there? Name one. - PowerPoint PPT Presentation

Transcript of Mobile Programming Lecture 5

Page 1: Mobile Programming Lecture 5

Mobile ProgrammingLecture 5

Composite Views, Activities, Intents and Filters

Page 2: Mobile Programming Lecture 5

Lecture 4 Review

• How do you get the value of a string in the strings.xml file?

• What are the steps to populate a Spinner or ListView using XML?

• How many Android application components are there? Name one.

• How do you launch an Activity B from within Activity A?

Page 3: Mobile Programming Lecture 5

Agenda

• ViewFlipper

• SlidingDrawer

• TabLayout

• Activity LifeCycle

• Configuration Changes

• URI

• Intent Filters

Page 4: Mobile Programming Lecture 5

ViewFlipper

A ViewFlipper allows you to switch between views that are children of the ViewFlipper

You can find it in the Transitions menu in Graphical Layout

Page 5: Mobile Programming Lecture 5

ViewFlipper<ViewFlipper android:id="@+id/myViewFlipper" android:layout_width="match_parent" android:layout_height="match_parent" >

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout>

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" >

</RelativeLayout>

</ViewFlipper>

Page 6: Mobile Programming Lecture 5

ViewFlipper<ViewFlipper android:id="@+id/myViewFlipper" android:layout_width="match_parent" android:layout_height="match_parent" >

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout>

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" >

</RelativeLayout>

</ViewFlipper>

Here I used RelativeLayouts, but you can place any widget you want in here.

Page 7: Mobile Programming Lecture 5

ViewFlipper<ViewFlipper android:id="@+id/myViewFlipper" android:layout_width="match_parent" android:layout_height="match_parent" >

<TextView android:layout_width="match_parent" android:layout_height=“wrap_content" />

<TextView android:layout_width="match_parent" android:layout_height=“match_parent“ / ></ViewFlipper>

Here I also used just 2 Views. You can add more than just 2 Views if you want to.

Page 8: Mobile Programming Lecture 5

ViewFlipper

ViewFlipper flipper;

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);flipper.setFlipInterval(500);flipper.startFlipping();

}

Page 9: Mobile Programming Lecture 5

ViewFlipper

ViewFlipper flipper;

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);flipper.setFlipInterval(500);flipper.startFlipping();

}

Here we set the flipper to flip every 500 milliseconds

Page 10: Mobile Programming Lecture 5

ViewFlipper

ViewFlipper flipper;

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

flipper.setOnClickListener(new OnClickListener() {

@Overridepublic void onClick(View v) {

flipper.showNext();}

});}

Here we set the flipper to flip when the flipper is clicked

Page 11: Mobile Programming Lecture 5

• FrameLayout is designed to block out an area on the screen to display a single item.

• FrameLayout should be used to hold a single child viewo it can be difficult to organize child views in a way

that's scalable to different screen sizes without the children overlapping each other

FrameLayout

Page 12: Mobile Programming Lecture 5

• Example:

FrameLayout

Page 13: Mobile Programming Lecture 5

SlidingDrawer

• hides content out of the screen and allows the user to drag a handle to bring the content on screen

• can be used vertically or horizontally

• composed of two children viewso the handle, that the users dragso the content, attached to the handle and dragged with it.

• should only be used inside of a FrameLayout or a RelativeLayout

Page 14: Mobile Programming Lecture 5

SlidingDrawer

• Note: the android:handle and android:content attributes

• There needs to be children with id's matching these attributes

Page 15: Mobile Programming Lecture 5

• android:handle

• android:content

• android:orientationo "vertical" or "horizontal"

• android:allowSingleTapo "true" or "false"o allow the user to open the drawer by tapping on the

handle?

SlidingDrawer - useful attributes

Page 16: Mobile Programming Lecture 5

• open()

• close()

• setOnDrawerScrollListener(OnDrawerScrollListener)

• setOnDrawOpenListener(OnDrawerOpenListener)

• setOnDrawerCloseListener(OnDrawerCloseListener)

SlidingDrawer - useful methods

Page 17: Mobile Programming Lecture 5

It is used to wrap multiple activities into a single window

• navigate through the activities using tabs

TabLayout

Page 18: Mobile Programming Lecture 5

TabLayout - Anatomy

TABHOST

TABWIDGET

ACTIVITY

FRAMELAYOUT

TAB CONTENT

• TabHosto container holding TabWidget and

a FrameLayout

• TabWidgeto row of tab buttons

• FrameLayouto container holding the tab contentso each tab content is a child of

FrameLayout

Page 19: Mobile Programming Lecture 5

TabLayout - XML<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout></TabHost>

TABHOST

TABWIDGET

ACTIVITY

FRAMELAYOUT

TAB CONTENT

Page 20: Mobile Programming Lecture 5

TabLayout - XML<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout></TabHost>

TABHOST

TABWIDGET

ACTIVITY

FRAMELAYOUT

TAB CONTENT

Tabs are different Activities, we can set and specify the layout for each tab programmatically

Page 21: Mobile Programming Lecture 5

TabLayout

• If you're going to have x number of tabs, create x number of Activities, 1 for each tab, in addition to the TabActivity (Host Activity).

• You can create x number of XML layouts for each tab, or you can reuse the same layout for each tab.

Page 22: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost(); // The activity TabHost

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Page 23: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity { @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost(); // The activity TabHost

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

extend TabActivity

Page 24: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost(); // The activity TabHost

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

the XML file containing the TabHost, TabWidget, FrameLayout

Page 25: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost(); TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Reference to the Activity's TabHost (which was defined in XML)

Page 26: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Reusable TabSpec for each Tab.

Page 27: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

The TabSpec tells the TabHost what views represent thetab contents and what the tab buttons should look like.

Page 28: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Remember from the previous lecture, this is how we use an Intent object to start another Activity

Page 29: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Refer to this TabActivity's tab host (which will contain the actual tabs)

Page 30: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Create a new tab spec, give it the id "linear layout"

Page 31: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Set the label for the tab (label the user sees) to "Linear". And

Page 32: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

We're not using an image for the tabs, so null for this argument

Page 33: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent); tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Fill the FrameLayout to hold the Activity specified by this intent

Page 34: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class);

spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent);

tabHost.addTab(spec);

}

Add the tab to the tabHost, it will now show up in the UI

Page 35: Mobile Programming Lecture 5

TabLayoutpublic class TabLayoutExampleActivity extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TabHost tabHost = getTabHost();

TabHost.TabSpec spec;

Intent intent = new Intent(TabLayoutExampleActivity.this, LinearLayout.class);

spec = tabHost.newTabSpec("linear layout").setIndicator("Linear", null).setContent(intent);

tabHost.addTab(spec);

Intent intent = new Intent(TabLayoutExampleActivity.this, TableLayout.class); spec = tabHost.newTabSpec("table layout").setIndicator("Table",null).setContent(intent); tabHost.addTab(spec);}

To add another tab, let's do this all over again!

Page 36: Mobile Programming Lecture 5

TabLayout - Rules

• � TabHost must have the id @android:id/tabhost

• �The TabWidget must have the id �@android:id/tabs

• �The FrameLayout must have the id �@android:id/tabcontent

Page 37: Mobile Programming Lecture 5

TabHost - useful methods

• setCurrentTab(int)o make this tab the active tab, making it visible in the

UI

• setOnTabChangedListener(OnTabChangedListener)o react to when the active tab changes

Page 38: Mobile Programming Lecture 5

Activity LifeCycle

• During the life of an activity, the system calls a core set of lifecycle methods in a sequence.

• Implementing your activity lifecycle methods properly ensures your app behaves gracefully in many ways, including that it:o Does not crash if the user receives a phone call or switches to another app while

using your app.o Does not consume valuable system resources when the user is not actively

using it.o Does not lose the user's progress if they leave your app and return to it at a later

time.

Page 39: Mobile Programming Lecture 5

Activity Lifecycle

• A nice picture of activity lifecycle• http://developer.android.com/reference/

android/app/Activity.html

Page 40: Mobile Programming Lecture 5

Activity LifeCycle - onCreate()

• Called when the activity is first created.

• this is where you should do all of your normal static set up: create views, bind data to lists, etc.

• provides you with a Bundle containing the activity's previously frozen state, if there was one.

• always followed by onStart().

Page 41: Mobile Programming Lecture 5

Activity LifeCycle - onStart()

• called when the activity is becoming visible to the user

• followed by o onResume() if the activity comes to the foreground

Page 42: Mobile Programming Lecture 5

Activity LifeCycle - onRestart()

• called after your activity has been stopped, prior to it being started again

• always followed by onStart()

Page 43: Mobile Programming Lecture 5

• called when the activity will start interacting with the user

• at this point your activity is at the top of the activity stack, with user input going to it

• Always followed by onPause()

Activity LifeCycle - onResume()

Page 44: Mobile Programming Lecture 5

• called when the system is about to start resuming a previous activity

• this is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc

• implementations of this method must be very quick because the next activity will not be resumed until this method returns

• followed by o onResume() if the activity returns back to the fronto onStop() if it becomes invisible to the user.

Activity LifeCycle - onPause()

Page 45: Mobile Programming Lecture 5

• called when the activity is no longer visible to the user because another activity has been resumed and is covering this one

• this may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed

• followed by o onRestart() if this activity is coming back to interact with

the usero onDestroy() if this activity is going away

Activity LifeCycle - onStop()

Page 46: Mobile Programming Lecture 5

• the final call you receive before your activity is destroyed

• this can happen either because o the activity is finishing (someone called finish() )o the system is temporarily destroying this instance of

the activity due to low memory

• you can distinguish between these two scenarios with the isFinishing() method

Activity LifeCycle - onDestroy()

Page 47: Mobile Programming Lecture 5

Reading Assignment

• Please read page 0 – 710 by next class. Pay more attention on the section of Large-Screen Strategies and Tactics.

• We are going to talk about Fragment next week.

Page 48: Mobile Programming Lecture 5

Configuration Changes

• In your app, you can detect when the configuration of the device changeso screen orientation, keyboard availability, and

language

• In default, the system will try to handle the changes for you, unless you specify that you want to handle them yourself

Page 49: Mobile Programming Lecture 5

Configuration Changes - Manifest

• to specify that, you want to handle orientation and keyboard availability changes by yourself

• open the manifest file and add the bold line<activity

android:configChanges="orientation|keyboardHidden"android:name=".OnConfigurationChangedExampleActivity"android:label="@string/app_name" ><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

Page 50: Mobile Programming Lecture 5

Configuration Changes - Manifest

• This specifies one or more configuration changes that the activity will handle itself when the orientation changes and when keyboard availability changes.

<activityandroid:configChanges="orientation|keyboardHidden"android:name=".OnConfigurationChangedExampleActivity"android:label="@string/app_name" ><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

Page 51: Mobile Programming Lecture 5

Configuration Changes - EventThen, to react to the orientation change event, add this method to your Activity@Overridepublic void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

/* ... */} else if (newConfig.orientation ==

Configuration.ORIENTATION_PORTRAIT) {/* ... */

}}

Page 52: Mobile Programming Lecture 5

Passing Data between Activities

When you start Activity B from Activity A, you may want to send data to Activity B

Activity A Activity B

startActivity B

send some data to B also

Page 53: Mobile Programming Lecture 5

Passing Data between Activities

Activity A - onCreate()

Intent intent = new Intent(this,

SecondActivity.class);

Bundle bundle = new Bundle();

bundle.putString("fname","John");

bundle.putString("lname", "Doe");

bundle.putInt("age", 18);

intent.putExtras(bundle);

startActivity(intent);

Activity B - onCreate()

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

if(bundle != null) {

edit1.setText(bundle.getString("fname"));

edit2.setText(bundle.getString("lname"));

int age= bundle.getInt("age");

}

Page 54: Mobile Programming Lecture 5

Explicit Intent vs Implicit Intent

• An Intent encapsulates a request, made to Android, for some activity or other receiver to do something.

• If the activity you intend to launch is one of your own, you may find it simplest to create an explicit Intent.o new Intent(this, MyListViewActivity.class);

• You can also start up activities from the operating system or third-party apps using implicit intent.o Implicit intent works a lot like the Web HTTP.

Page 55: Mobile Programming Lecture 5

Implicit Intent

• Implicit Intent o Action + URI (“data”)o these are almost exactly analogous to HTTP verbs (POST,

GET) and URLs — the action is the verb, and the “ data” is a Uri, such as http://commonsware.com

• android.intent.action:o MAINo MUSIC_PLAYERo VIEWo WEB_SEARCH

Page 56: Mobile Programming Lecture 5

URIs

• Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC 2396.

• The URI class can both parse URI strings into parts and compose URI strings from parts.

• There are 4 main parts to a URIo Scheme, port, host, and path

• http://developer.android.com/reference/java/net/URI.html

Page 57: Mobile Programming Lecture 5

URIs - Examples - Hierarchical

http://mobile.cs.fsu.edu/androidhttp://twitter.comfile:///tmp/android.txt

scheme host port pathhttp mobile.cs.fsu.edu 80 android

http twitter.com

file /tmp/android.txt

Page 58: Mobile Programming Lecture 5

URIs - Examples - Opaque

mailto:robots.example.com

scheme scheme-specific partmailto robots.example.com

Page 59: Mobile Programming Lecture 5

URIs - Parsing URIsURI uri = Uri.parse("http://www.google.com");

String scheme = uri.getScheme();

String host = uri.getHost();

int port = uri.getPort();

String path = uri.getPath();

String schemeSpecificPart = uri.getSchemeSpecificPart();

Page 60: Mobile Programming Lecture 5

Intent Filters

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters• each filter describes a capability of the component, a

set of intents that the component is willing to receive

• it filters in intents of a desired type, while filtering out unwanted intents — but only unwanted implicit intents (those that don't name a target class)

Page 61: Mobile Programming Lecture 5

Intent Filters

• explicit intento always delivered to its target, no matter what it

contains; the filter is not consulted

• implicit intento delivered to a component only if it can pass through

one of the component's filters

Page 62: Mobile Programming Lecture 5

Intent Filters

• explicit intento always delivered to its target, no matter what it

contains; the filter is not consulted

• implicit intento delivered to a component only if it can pass through

one of the component's filters

We have seen this before!new Intent(A.this, B.class)

Page 63: Mobile Programming Lecture 5

Intent Filters

How does Android know that you may want to open the YouTube app when you try to watch a video on YouTube?

Using Intent Filters

We will create an app that can be used to launch links at

o http://mobile.cs.fsu.edu

Page 64: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Page 65: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Page 66: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Page 67: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"

android:host="mobile.cs.fsu.edu" /><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Display data to the user. Generic action you can use on a piece of data to get the most reasonable thing to occur

Page 68: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category

android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Set if the Activity should be an option for the default action on a piece of data.

Page 69: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

Activities that can be safely invoked from a browser must support this category. It's required here.

Page 70: Mobile Programming Lecture 5

Intent Filters

<activityandroid:name=".MyActivity"android:label="@string/app_name" ><intent-filter>

<data android:scheme="http"android:host="mobile.cs.fsu.edu" />

<action android:name="android.intent.action.VIEW" /><category

android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent.category.BROWSABLE"></category>

</intent-filter></activity>

This is an Implicit Intent!

Page 71: Mobile Programming Lecture 5

References

• The Busy Coder's Guide to Android Development - Mark Murphy

• Android Developers

• The Mobile Lab at Florida State University