当前位置:操作系统 > 安卓/Android >>

Android新手入门教程(十八):使用Intent调用“内置”应用の配置Intent-Filter

我们已经知道,一个activity通过使用Intent对象调用另外一个activity。为了能让其他activity做出回应,还需要在AndroidManifest.xml中配置<intent-filter>元素,同时指定action和category。例如:

[java] <intent-filter > 
    <action android:name="net.learn2develop.SecondActivity" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
            <intent-filter >
                <action android:name="net.learn2develop.SecondActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>


1.新建一个工程,创建一个类:MyBrowserActivity.java。同时在res/layout中创建一个xml文件:brwoser.xml。

2.AndroidManifest.xml


[java] <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.learn2develop.Intents" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="14" /> 
    <uses-permission android:name="android.permission.CALL_PHONE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:label="@string/app_name" 
            android:name=".IntentsActivity" > 
            <intent-filter > 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
         
               
        <activity android:name=".MyBrowserActivity" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.VIEW" /> 
                <action android:name="net.learn2develop.MyBrowser" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:scheme="http" /> 
            </intent-filter> 
        </activity> 
         
             
    </application> 
 
</manifest> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.Intents"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".IntentsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
             
        <activity android:name=".MyBrowserActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="net.learn2develop.MyBrowser" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
            </intent-filter>
        </activity>
       
           
    </application>

</manifest>3.在main.xml中添加一

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,