zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    阿里云OSS进行文件下载时,报NOSuchKeys: com.aliyun.oss.OSSException: The specified key does not exist.
    [JAVA异常]ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit erro
    mybatis 中的<![CDATA[ ]]>
    HttpClients.custom的创建
    RestTemplate可以自定义重试次数
    RegxUtils正则表达式工具类
    MYSQL中 != 和 is not的区别
    ccna ccnp ccie 区别
    【IDEA】IDEA SpringBoot访问不到webapp下的内容
    日志 | logback | logback-spring.xml
  • 原文地址:https://www.cnblogs.com/afluy/p/3394003.html
Copyright © 2011-2022 走看看