首页 > 代码库 > android ComponentName

android ComponentName

从一个activity跳转到另一个activity有多种方法

ComponentName

 //此为一个按钮相应的代码摘录       
ComponentName comp = new ComponentName(ComponentAttr.this, SecondActivity.class); //设置跳转的activityIntent intent = new Intent(); // 为Intent设置Component属性intent.setComponent(comp);startActivity(intent);


跳转的页面能够获取到ComponentName的相关信息

ComponentName comp = getIntent().getComponent();
// 显示该ComponentName对象的包名、类名
show.setText("组件包名为:" + comp.getPackageName()
+ "\n组件类名为:" + comp.getClassName());

 

 

使用intent 的action 属性 和 <intent-filter?

public final static String CRAZYIT_ACTION ="org.crazyit.intent.action.CRAZYIT_ACTION"

//此为按钮响应 
Intent intent = new Intent(); // 为Intent设置Action属性(属性值就是一个普通字符串) intent.setAction(ActionAttr.CRAZYIT_ACTION); startActivity(intent);

跳转到的activity 在配置文件中增加了

<activity android:name=".SecondActivity"
android:label="@string/app_name">
  <intent-filter>
  <!-- 指定该Activity能响应Action为指定字符串的Intent -->
  <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
  <!-- 指定该Activity能响应Action属性为helloWorld的Intent -->
  <action android:name="helloWorld" />
  <!-- 指定该Action能响应Category属性为指定字符串的Intent -->
  <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

 

intent action category

// 定义一个Action常量
final static String CRAZYIT_ACTION = "org.crazyit.intent.action.CRAZYIT_ACTION";
// 定义一个Category常量
final static String CRAZYIT_CATEGORY ="org.crazyit.intent.category.CRAZYIT_CATEGORY";


//跳转代码
Intent intent = new Intent(); // 设置Action属性intent.setAction(ActionCateAttr.CRAZYIT_ACTION); // 添加Category属性 intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY);startActivity(intent);

//配置文件

<activity android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<!-- 指定该Activity能响应action为指定字符串的Intent -->
<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
<!-- 指定该Activity能响应category为指定字符串的Intent -->
<category android:name="org.crazyit.intent.category.CRAZYIT_CATEGORY" />
<!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

 

intent 调用系统通讯录

    Intent intent = new Intent();     //设置Intent的Action属性    intent.setAction(Intent.ACTION_GET_CONTENT);      //设置Intent的Type属性    intent.setType("vnd.android.cursor.item/phone");     // 启动Activity,并希望获取该Activity的结果     startActivityForResult(intent, PICK_CONTACT);


回调方法

public void onActivityResult(int requestCode        , int resultCode, Intent data)    {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode)        {            case (PICK_CONTACT):                if (resultCode == Activity.RESULT_OK)                {                    // 获取返回的数据                    Uri contactData =http://www.mamicode.com/ data.getData();                    CursorLoader cursorLoader = new CursorLoader(this                        , contactData, null, null, null, null);                    // 查询联系人信息                    Cursor cursor = cursorLoader.loadInBackground();                    // 如果查询到指定的联系人                    if (cursor.moveToFirst())                    {                        String contactId = cursor.getString(cursor                            .getColumnIndex(ContactsContract.Contacts._ID));                        // 获取联系人的名字                        String name = cursor.getString(cursor                            .getColumnIndexOrThrow(                            ContactsContract.Contacts.DISPLAY_NAME));                        String phoneNumber = "此联系人暂未输入电话号码";                        //根据联系人查询该联系人的详细信息                        Cursor phones = getContentResolver().query(                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                            null,                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID                                + " = " + contactId, null, null);                        if (phones.moveToFirst())                        {                            //取出电话号码                            phoneNumber = phones                                .getString(phones                                .getColumnIndex(ContactsContract                                .CommonDataKinds.Phone.NUMBER));                        }                        // 关闭游标                        phones.close();                        EditText show = (EditText) findViewById(R.id.show);                        //显示联系人的名称                        show.setText(name);                        EditText phone = (EditText) findViewById(R.id.phone);                        //显示联系人的电话号码                        phone.setText(phoneNumber);                    }                    // 关闭游标                    cursor.close();                }                break;        }    }

 

android ComponentName