Pro Android Learning Note (10): Learn Intent (UP)

Source: Internet
Author: User

Android introduced the concept of intent to evoke components,component including:
1. Activity (UI Element)
2. Service (Background code)
3. Broadcast receiver (code to process broadcast messages)
4. Content Provider (code for abstract data)

Intent Basic meaning

Intent is the action that notifies the platform to process (evoke). The action that Android evokes will depend on what action is registered. For example, we have a simple activity:intentbaiscviewactivity. In Androidmanifest, we register the action, allowing other apps to invoke the activity.

<activity android:name= ". Intentbasicviewactivity "android:label=" @string/intent_basic_test ">
<intent-filter>
<!--the name of the action registered is. Intent.action.YOUR_ACTION_NAME. (Test: If you do not use Package-name, there will be no activity exceptions found on the call)--
<action android:name= "Cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

The call is made in the following ways:

String actionname = "Cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView";
Intent i = new Intent (actionname);
Showinfo ("Invoke Intent: \n\t" + i);
This. startactivity (i);

In the evoked intentbaiscviewactivity, you can find the intent object that evokes him, as follows:

Intent i = this.getintent ();
Showinfo ("Invoke Intent: \n\t" + i.tostring ());

Showinfo () is the display of information in TextView, as shown in this example

intent of the system

Some applications of the Android system can be invoked via intent, and the specific usage is shown in http://developer.android.com/guide/appendix/g-app-intents.html. The following small example will experiment with these calls. In this example, Optionmenu is used to select the specific calling application. There is a menu directory under the Layout resource of Android, we recommend an XML file below, and also list the group and item of menu. Of course we can also write in code (see: Android Learning Note (eight): Activity-openmenu and LinearLayout).

<?xml version= "1.0" encoding= "Utf-8"?>
<MenuXmlns:android= "Http://schemas.android.com/apk/res/android" >
<group android:id= "@+id/intentmenu_main" >
<item android:id= "@+id/intent_menu_browser" android:title=
"
@string/intent_menu_browser"/>
<item android:id= "@+id/intent_menu_search" android:title= "@string/intent_menu_search"/>
<item android:id= "@+id/intent_menu_dial" android:title= "@string/intent_menu_dial"/>
<item android:id= "@+id/intent_menu_call" android:title= "@string/intent_menu_call"/>
<item android:id= "@+id/intent_menu_map" android:title= "@string/intent_menu_map"/>
</Group>
</menu>

The Java code is as follows:

public class Intenttestdemo extends activity{
... ...
protected void OnCreate (Bundle savedinstancestate) {
......
}

//"Menu" 1,Creating Optionmenu requires rewriting oncreateoptionsmenu, and then getting information from an XML resource and building a menu through Menuinflater
public BooleanOncreateoptionsmenu(Menu menu) {
Super.oncreateoptionsmenu (menu);
menuinflater mi = getmenuinflater ();
Mi.inflate
(R.menu.intent_test_menu, menu);
return true;
}

//"Menu" 2,Select the user to select a menu to trigger onoptionsitemtselected (), in this case we set out according to different options, different system applications.
public boolean onoptionsitemselected (MenuItem item) {
try{
SwitchItem.getitemid ()){
Case R.id.intent_menu_browser:The ID in the corresponding XML
Invokebrowser ();
Break
Case R.id.intent_menu_search:
Invokesearch ();
Break
Case R.id.intent_menu_dial:
Invokedial ();
Break
Case R.id.intent_menu_call:
Invokecall ();
Break
Case R.ID.INTENT_MENU_MAP:
Invokemap ();
Break
Default
LOG.D ("Pro", "Get option error.");
Break
}
}catch (Exception e) {
LOG.D ("Pro", e.tostring ());
}
return true;
}
"Intent" triggers the browser to open the specified Web page, and for Action_view, the system will invoke different applications based on the format of the passed URI, http://,https://when called browser
private void Invokebrowser () {
Intent Intent = new Intent (Intent.action_view);
Intent.SetData(Uri.parse ("http://www.google.com.hk"));
StartActivity (Intent);
}
"Intent" is also the trigger browser, but the name of the network search, and the above Action_view different, run the transmission of data "" (empty string), because Action_view corresponds to a number of possible applications, and web_ Search only corresponds to browser.
private void Invokesearch () {
Intent Intent = new Intent (Intent.action_web_search);
Intent.setdata (Uri.parse ("http://www.google.com.hk"));
StartActivity (Intent);
}
"Intent" opens the Dial UI, which can be preset tel:phone_number, or voicemail:xxxxx.
private void Invokedial () {
Intent Intent = new Intent (intent.action_dial);
StartActivity (Intent);
}
"Intent" to open the Dial UI, while exhaling tel:phone_number, and action_dial need users to press the dial key different, Action_call direct outbound, need authorization Android.permission_call_ PHONE
private void Invokecall () {
Intent Intent = new Intent (Intent.action_call);
Intent.setdata (Uri.parse ("tel:02012345678"));
StartActivity (Intent);
}
"Intent" gives the location, Action_view Select and trigger Google Map, provided Google Map is already installed. Before we installed Huawei's smart cloud on the simulator, there was an app store that was more convenient to install directly from there. Also if the URI is google.streetview:cbll=lat,LNG&cbp=1,Yaw,,Pitch,Zoom&mz=Mapzoom,View will call Google Streetview.
private void Invokemap () {
Intent Intent = new Intent (Intent.action_view);
Intent.setdata (uri.parse ("geo:23.0,123.0"));
Intent.setdata (Uri.parse ("Geo: 23.0,123.0?z=4&q=business+near+city ")); Add parameters such as Zoom
StartActivity (Intent);
}

}

RELATED Links: My Android development related articles

Pro Android Learning Note (10): Learn Intent (UP)

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.