美文网首页
创建第二个activity

创建第二个activity

作者: yanghanbin_it | 来源:发表于2017-06-08 15:00 被阅读0次
    创建第二个activity
    public class SecondActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    }  
    
    将新创建的activity添加进清单文件
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.secondactivity"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="21" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category. LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="com.example.secondactivity.SecondActivity" >
            </activity>
        </application>
    </manifest>  
    

    如果Activity有以下代码,则会创建应用图标,如果多个activity都有,则会出现多个应用图标

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    
    如果Activity所在包名与应用的名相同,可以不写包名
    如果一个activity需要隐式跳转,那么必须在清单文件中添加以下配置
    <activity android:name="com.example.activeswitch.SecondActivity" >
        <intent-filter>
            <action android:name="com.example.activeswitch.second" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>  
    
    • action节点的name是自己定义的,定义好之后,这个name的值,会成为activity的动作,在隐式启动activity时,意图中设置的action必须与 com.example.activeswitch.second 一致

    Activity跳转的配置需要注意:

    • 跳转的代码中需要与intent-filter里面的配置一直
      • 如intent-filter中配置的action, category,在跳转中必须一直,如果category的配置为 android.intent.category.DEFAULT 在跳转代码中可不写

    例1:

    <activity android:name="com.example.activeswitch.SecondActivity" >
        <intent-filter>
            <action android:name="com.example.activeswitch.second" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    
    // action名和category必须与xml中配置一样
    public void click4(View v) {
            Intent intent = new Intent();
            intent.setAction("com.example.activeswitch.second");
            //如果category配置为android.intent.category.DEFAULT,系统会添加默认的category,这里可以省略不写
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }  
    

    例2:

    <activity android:name="com.example.activeswitch.SecondActivity" >
                <intent-filter>
                    <action android:name="com.example.activeswitch.second" />
                    <data android:scheme="hello"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>  
    
    // action名和data的scheme一样,且必须与以scheme配置为前缀
    public void click4(View v) {
            Intent intent = new Intent();
            intent.setAction("com.example.activeswitch.second");
            intent.setData(Uri.parse("hello:123"));
            startActivity(intent);
        }  
    

    例3

    <activity android:name="com.example.activeswitch.SecondActivity" >
                <intent-filter>
                    <action android:name="com.example.activeswitch.second" />
                    <data android:mimeType="text/username"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>  
    
    //必须与action名和data的mineType保存一致
    public void click4(View v) {
            Intent intent = new Intent();
            intent.setAction("com.example.activeswitch.second");
            intent.setType("text/username");
            startActivity(intent);
        }  
    

    例4

    <activity android:name="com.example.activeswitch.SecondActivity" >
                <intent-filter>
                    <action android:name="com.example.activeswitch.second" />
                    <data android:mimeType="text/username" android:scheme="hello"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>  
    
    // action和data中的scheme和mimeType必须保存一致
    public void click4(View v) {
            Intent intent = new Intent();
            intent.setAction("com.example.activeswitch.second");
            intent.setDataAndType(Uri.parse("hello:123"), "text/username");
            startActivity(intent);
        }  
    
    • action的多个启动方式
    <activity android:name="com.example.activeswitch.SecondActivity" >
                <intent-filter>
                    <action android:name="com.example.activeswitch.second" />
                    <action android:name="com.example.activeswitch.second3" />
                    <data android:scheme="hello"/>
                    <data android:scheme="hello3"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                
                <intent-filter>
                    <action android:name="com.example.activeswitch.second2" />
                    <data android:scheme="hello2"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>  
    

    以上的配置方式可以通过以下的方式进行启动

    public void click4(View v) {
            // 方式一可行 同在一个intent-filter下 
            Intent intent = new Intent();
            intent.setAction("com.example.activeswitch.second");
            intent.setDataAndType(Uri.parse("hello:123"), "text/username");
            startActivity(intent);
            // 方式二可行 同在一个intent-filter下 
            Intent intent2 = new Intent();
            intent2.setAction("com.example.activeswitch.second3");
            intent2.setDataAndType(Uri.parse("hello:123"), "text/username");
            startActivity(intent2);
            // 方式三可行 同在一个intent-filter下 
            Intent intent3 = new Intent();
            intent3.setAction("com.example.activeswitch.second");
            intent3.setDataAndType(Uri.parse("hello3:123"), "text/username");
            startActivity(intent3);
            
            // 方式四可行 同在一个intent-filter下 
            Intent intent4 = new Intent();
            intent4.setAction("com.example.activeswitch.second2");
            intent4.setDataAndType(Uri.parse("hello2:123"), "text/username");
            startActivity(intent4);
            
            //方式不可行, 不在同一个intent-filter下
            Intent intent5 = new Intent();
            intent5.setAction("com.example.activeswitch.second2");
            intent5.setDataAndType(Uri.parse("hello3:123"), "text/username");
            startActivity(intent5);
        }  
    

    相关文章

      网友评论

          本文标题:创建第二个activity

          本文链接:https://www.haomeiwen.com/subject/rfftqxtx.html