ANDROID笔记:Activity的显式和隐式调用

package com.example.android_activity.test;



import com.example.android_activity.R;



import android.app.LauncherActivity;

import android.content.ComponentName;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.widget.ArrayAdapter;



public class MyTestActivity extends LauncherActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(

                getApplicationContext(), android.R.layout.simple_list_item_1,

                new String[] { "com1", "com2" });

        setListAdapter(adapter);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



    @Override

    protected Intent intentForPosition(int position) {

        // TODO Auto-generated method stub

        Intent intent = null;

        switch (position) {

        case 0:

            // 显式声明方式1

            intent = new Intent(MyTestActivity.this, CompActivity.class);

            // 显式声明方式2

            ComponentName componentName = new ComponentName(

                    MyTestActivity.this, CompActivity.class);

            intent.setComponent(componentName);

            // 显式声明方式3

            ComponentName componentName2 = new ComponentName(

                    "com.example.android_activity.test.MyTestActivity",

                    "com.example.android_activity.test.CompActivity");

            intent.setComponent(componentName);

            break;

        case 1:

            intent = new Intent();

            //隐式

            intent.setAction("com.ex.one");

            intent.addCategory("android.intent.category.DEFAULT");

            break;

        }

        return intent;



    }

}

 

 

 

隐式调用时需要在AndroidManifest.xml中配置该activity的action和category,

该方式可以使其他程序调用该程序的activity

 

 <activity

            android:name="com.example.android_activity.test.CompActivity"

            android:label="@string/app_name"

          

            android:exported="true"

             >

              <!-- android:exported="true" 其他程序可以调用  -->

            <intent-filter>

                <action android:name="com.ex.one" />

    

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

 

你可能感兴趣的:(Activity)