Intent Basic Android SampleAndroid Code Samples [April 04, 2013 at 12:41 PM] 2,080 views
1. Introducing Android Intent
Intents are used as a message-passing mechanism that lets you declare your intention that an action be performed, usually with (or on) a particular piece of data.
Explicit intents explicitly defines the component which should be called by the Android system, by using the Java class as identifier.
Intent i = new Intent(this, SecondActivity.class); i.putExtra("Key1", "This value one for SecondActivity"); i.putExtra("Key2", "This value two SecondActivity");
Explicit intents are typically used within on application as the classes in an application are controlled by the application developer.
1.2. Implicit Intent
Implicit intents specify the action which should be performed and optionally data which provides data for the action.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:555-2134")); startActivity(intent);
2. Using Intent
2.1. Launching to Activities
To start Activities, we must make two steps as follow:
<activity android:name=".SecondActivity" android:label="@string/title_activity_second" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Step 2: Create Intent to call the Activity
Intent secondActivity = new Intent(this, SecondActivity.class); startActivity(secondActivity);
2.2. Start Service
To start Services, we must make two steps as follow:
<service android:name="MyService" > </service>
Step 2: Create Intent to call the Service.
Intent myServiceIntent = new Intent(this, MyService.class); startService(myServiceIntent);
2.3. Send BroadcastReceiver
To start BroadcastReceiver, we must make two steps as follow:
<receiver android:name="MyBroadcastReceiver" > <intent-filter> <action android:name="com.example.intenttutorial.myrecevier" /> </intent-filter> </receiver>
Step 2: Create Intent to send the BroadcastReceiver
Intent broadcastIntent = new Intent(broadcastFilter); broadcastIntent.putExtra("9Android.net","MyBroadcastReciver is called!"); sendBroadcast(broadcastIntent);
3. Sample Code
This is very simple application to show you some easies way to use Intent. It ‘s very easy to understand.
Create XML Layout: activity_main.xml
Declaration in Manifest
Tags: Android Samples