Intent Component,action and Category Properties-android learning Journey (50)

Source: Internet
Author: User

Component Property



code example
 Public  class mainactivity extends Activity{    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Findviewbyid (r.id.btnintent). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {ComponentName name =NewComponentName (mainactivity. This, Mainactivity2.class); Intent Intent =NewIntent ();                Intent.setcomponent (name);            StartActivity (Intent);    }        }); }

Action and Category properties and Intentfilter configuration




code example
 Public  class mainactivity extends Activity{     Public  Final StaticString 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 (NewView.onclicklistener () {@Override             Public void OnClick(View view) {Intent Intent =NewIntent ();                Intent.setaction (mainactivity.liupeng_action);            StartActivity (Intent);    }        }); }}
<activityandroid: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>
Introduction to the action and category of the system



View and get system Contact data Demo
 Public  class mainactivity extends Activity{    Final intPick_contact =0;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Findviewbyid (r.id.btncontacts). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View view) {Intent Intent =NewIntent ();                Intent.setaction (intent.action_get_content); Intent.settype ("Vnd.android.cursor.item/phone");            Startactivityforresult (intent,pick_contact);    }        }); }@Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Super. Onactivityresult (Requestcode, ResultCode, data);if(Requestcode = = Pick_contact && ResultCode = = RESULT_OK)            {Uri contactsdata = Data.getdata (); Cursorloader Cursorloader =NewCursorloader ( 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 ="This contact has not entered a number"; 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 (); }    }}
Layout
<?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  =;   <edittext  android:layout _width  = "fill_parent"  android:layout_height< /span>= "wrap_content"  android:id  = "@+id/show"  android:editable  = "False"  android:cursorvisible  = "false" />     <edittext  android:layout _width  = "fill_parent"  android:layout_height< /span>= "wrap_content"  android:id  = "@+id/phone"  android:editable  =         android:cursorvisible  = "false" />     <buttonandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:id="@+id/btncontacts"android:text="View Contacts"/>                                </linearlayout>
Back to Home Desktop 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 (NewView.onclicklistener () {@Override             Public void OnClick(View view) {Intent Intent =NewIntent ();                Intent.setaction (Intent.action_main);                Intent.addcategory (Intent.category_home);            StartActivity (Intent);    }        }); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Intent Component,action and Category Properties-android learning Journey (50)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.