Intent的Component,Action和Category属性详解-android学习之旅(五十)

时间:2021-07-04 03:49:28

Component属性

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

代码示例

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnIntent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ComponentName name = new ComponentName(MainActivity.this,MainActivity2.class);
                Intent intent = new Intent();
                intent.setComponent(name);
                startActivity(intent);
            }
        });
    }

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Action和Category属性以及IntentFilter配置

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

代码示例

public class MainActivity extends Activity{
    public  final static String LIUPENG_ACTION ="liu.intent.action.liuaction";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnIntent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(MainActivity.LIUPENG_ACTION);
                startActivity(intent);
            }
        });
    }
}
<activity
            android:name=".MainActivity2"
            android:label="@string/title_activity_main_activity2" >
            <intent-filter>
                <action android:name="liu.intent.action.liuaction"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

系统的Action和Category介绍

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

Intent的Component,Action和Category属性详解-android学习之旅(五十)

察看并获取系统联系人数据demo

public class MainActivity extends Activity{
    final int PICK_CONTACT = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnContacts).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("vnd.android.cursor.item/phone");
                startActivityForResult(intent,PICK_CONTACT);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_CONTACT && resultCode == RESULT_OK){
            Uri contactsData = data.getData();
            CursorLoader cursorLoader = new CursorLoader(this,contactsData,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)findViewById(R.id.show)).setText(name);
                ((EditText)findViewById(R.id.phone)).setText(phoneNumber);
            }
            cursor.close();
        }
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
  <EditText
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:id="@+id/show"
      android:editable="false"
      android:cursorVisible="false"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone"
        android:editable="false"
        android:cursorVisible="false"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnContacts"
        android:text="察看联系人"/>

</LinearLayout>

返回Home桌面demo

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnContacts).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            }
        });
    }
}