0% found this document useful (0 votes)
82 views

Intents & Intent Filters

Intents allow interaction between application components in Android and support actions like starting a new activity, broadcasting messages, and more. Intent filters are required to match intents to activities, services, and broadcast receivers. They are usually declared in the Android manifest file. Broadcast intents allow broadcasting messages between components, making applications more open. Broadcast receivers can listen for broadcast intents using intent filters.

Uploaded by

John Green
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Intents & Intent Filters

Intents allow interaction between application components in Android and support actions like starting a new activity, broadcasting messages, and more. Intent filters are required to match intents to activities, services, and broadcast receivers. They are usually declared in the Android manifest file. Broadcast intents allow broadcasting messages between components, making applications more open. Broadcast receivers can listen for broadcast intents using intent filters.

Uploaded by

John Green
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Intents & Intent Filters

codeandroid.org

Intents & Intents Filter


Intents : request for an action to be
performed (usually on a set of data) and Broadcast Receivers (as being capable of performing an action on a set of data)

Intent Filters : register Activities, Services, Broadcast Receivers : listens to intents

Intents
Support interaction between any
application components available on an Android device

start a new Activity broadcast messages (broadcast intents)

Starting a new Activity


Intent intent = new Intent (......................); startActivity(intent);

Intents

Starting a new Activity


Dial a number

Intents

Intent intent = new Intent (Intent.ACTION_DIAL, Uri.parse(tel:93675359)); startActivity(intent);


Launch a website

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(http://codeandroid.org)); startActivity(intent);

Starting a new Activity


Launch an Activity

Intents

Intent intent = new Intent (this, HelloWorld.class); startActivity(intent);


Launch an Activity

Intent intent = new Intent (this, HelloWorld.class); intent.putExtra(title,Hello codeandroid); startActivity(intent);

Intent Filters
AndroidManifest.xml

<activity android:name=.HelloWorld android:label=@string/app_name> <intent-lter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/> </intent-lter> </activity>
Launch Hello World

Intent intent = new Intent (this, HelloWorld.class); startActivity(intent);

Intent Filters
Required for Intent resolution to
match Intents to Activities, Services, or BroadcastReceivers AndroidManifest.xml of an application

Most Intent Filters are declared in

Intent Filters
AndroidManifest.xml

<activity android:name=.HelloWorld android:label=@string/app_name> <intent-lter> <action android:name=org.codeandroid.intentstest.HelloWorld/> <category android:name=android.intent.category.DEFAULT/> </intent-lter> </activity>
Launch Hello World

Intent intent = new Intent (org.codeandroid.intentstest.HelloWorld); startActivity(intent);

Intent Filters
AndroidManifest.xml <activity android:name=.HelloWorld android:label=@string/app_name> <intent-lter> <action android:name=android.intent.action.VIEW/> <category android:name=android.intent.category.DEFAULT/> <category android:name=android.intent.category.BROWSABLE/> <data android:scheme=http android:host=androidium.org/> </intent-lter> </activity> Launch Hello World

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(http://androidium.org)); startActivity(intent);

Intent Filters
AndroidManifest.xml <activity android:name=.HelloWorld android:label=@string/app_name> <intent-lter> <action android:name=android.intent.action.VIEW/> <category android:name=android.intent.category.DEFAULT/> <category android:name=android.intent.category.BROWSABLE/> <data android:scheme=http android:host=www.google.com android:pathPrex=/advanced_search /> </intent-lter> </activity>

Intent Filters
AndroidManifest.xml <activity android:name=.HelloWorld android:label=@string/app_name> <intent-lter> <action android:name=android.intent.action.VIEW/> <category android:name=android.intent.category.DEFAULT/> <category android:name=android.intent.category.BROWSABLE/> <data android:scheme=codeandroid/> </intent-lter> </activity> Launch Hello World

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(codeandroid://)); startActivity(intent);

Launch the Android Market


Uri marketUri = Uri.parse(http://market.android.com/search?q=pname:com.buuuk.buUuk) Intent intent = new Intent (Intent.ACTION_VIEW, marketUri); startActivity(intent);

Intents

Uri marketUri = Uri.parse(market://search?q=pname:com.buuuk.buUuk) Intent intent = new Intent (Intent.ACTION_VIEW, marketUri); startActivity(intent);

Broadcast Intents

Intents

broadcast messages between

components with the sendBroadcast method broadcasting to current and other applications

makes an application more open, by

Broadcast Intents

Intents

Intent intent = new Intent(org.codeandroid.intentstest.TestBroadcastReceiver); sendBroadcast(intent);

Broadcast Receivers
listen to Broadcast Intents must be registered (either in code or
within the app manifest

use Intent Filter to specify which


Intents it is listening for

Broadcast Receivers
registered inside code
IntentFilter lter = new IntentFilter(org.codeandroid.intentstest.TestBroadcastReceiver); TestBroadcastReceiver receiver = new TestBroadcastReceiver(); registerReceiver(receiver, lter);
public class TestBroadcastReceiver extends Broadcast Receiver { @Override public void onReceive(Context context, Intent intent) {
(.................... do something here................)

} }

Broadcast Receivers
register in app manifest
<receiver android:name=CameraPressedReceiver> <intent-lter> <action android:name=android.intent.action.CAMERA_BUTTON /> </intent-lter> </receiver> public class CameraPressed extends Broadcast Receiver { @Override public void onReceive(Context context, Intent intent) { } }
(.................... do something here................)

You might also like