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>
  • 相关阅读:
    AngularJS数据建模(转载)
    Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
    EF开发程序经常用的几个东西
    jQuery插件---轻量级的弹出窗口wBox
    SQL Server 索引维护sql语句
    windows server2008 r2 下启用 sqlserver 2008的远程连接
    windows2012 r2 提高网速方法
    jQuery validation
    Bootstrap Modal 垂直居中
    shell 循环读取文件及字符串转为数组
  • 原文地址:https://www.cnblogs.com/afluy/p/3394003.html
Copyright © 2011-2022 走看看