Wednesday, 6 May 2015

Lesson 10 Using Android Intents Tutorial ---- 4

Using Android Intents Tutorial

Categorically Speaking

We have added each of our activities to the Android Manifest. At this point I want to look at what we currently have in the manifest.
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learnandroid.intents"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="3" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".UsingIntentsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="GetName"></activity>
        <activity android:name="UriActivity"></activity>
    </application>
 
</manifest>
You’ll notice two of our activities are simple tags with just a name.
        <activity android:name="GetName"></activity>
        <activity android:name="UriActivity"></activity>
But one of our activities has a section named intent-filter
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
We saw actions in the last section (and this is how you tell android what actions your activity can perform), but the category is new. This combination of the action “android.intent.action.MAIN” and the category “android.intent.category.LAUNCHER” tells android that this is the main activity that should be launched when the user touches the application icon in the launcher. We can use the action and category to limit what is affected by our intent. So let’s add a category and action to our other two activities now.
We can use whatever we want for our action and our category. The Android developers recommend that if you use your own action or your own category that you prefix it with your package name, to prevent accidentally using the same action as another application on the phone. Here is our Manifest file now.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learnandroid.intents"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="3" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".UsingIntentsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="GetName" android:label="The Activity You Know">
            <intent-filter >
                <action android:name="com.learnandroid.intents.VIEW_SAMPLE_CODE" />
                <category android:name="com.learnandroid.intents.SAMPLE_CODE"/>                
            </intent-filter>
        </activity>
        <activity android:name="UriActivity" android:label="The URI You Know">
            <intent-filter >
                <action android:name="com.learnandroid.intents.VIEW_SAMPLE_CODE" />
                <category android:name="com.learnandroid.intents.SAMPLE_CODE"/>                
            </intent-filter>
        </activity>
    </application>
 
</manifest>
You might have noticed that I also added labels for our Activities. We’re going to use the actions, categories, and labels in the Android Manifest to automatically update the list in our main activity, replacing the hard coded array we have been using.

No comments:

Post a Comment