I. Intent Functions
Intent is frequently used in Android. Intent is used for system functions, whether it is page transfer, data transfer, or external program call.
Android has three basic components: Activity, service, and broadcastreceiver. They are all activated through the intent mechanism, and different types of components have different ways to transmit intent.
(1) to activate a new activity or perform a new operation on an existing activity, you can call the context. startactivity () or activity. startactivityforresult () method. The intent parameters required for these two methods are also called activity action intent (activity action intent). Based on the intent object's different description of the target activity, to start the activity or transfer information that matches it.
(2) To start a new service, or pass a new command to an existing service, call context. startservice () method or call context. the bindservice () method binds the context object that calls this method to the service.
(3) Send broadcastintent through context. sendbroadcast (), context. sendorderbroadcast (), and context. Send-stickbroadcast. After broadcastintent is sent, all registered broadcastreceiver with intentfilter matching it will be activated. This mechanism is widely used in notifications of device or system status changes. A common example is that when the battery_low power of Android is too low, the system sends a broadcast with the action as battery_low, then, any broadcastreceiver registered by intentfilter that matches the action will run custom processing code, such as disabling WiFi and GPS of the device to save battery consumption.
Once an intent is sent, Android will accurately find one or more matching activities, services, or broadcast-Uploader as a response. Therefore, different types of intent messages do not overlap: broadcastintent messages are sent only to broadcastreceiver, but cannot be sent to activity or service. Messages transmitted by startactivity () can only be sent to activity. Intent transmitted by startservice () can only be sent to service.
2. Use intent to transmit data between multiple activities
First, we will focus on how to use intent to transmit data between multiple activities. According to the previous introduction, we should know that to transfer data from activity1 to activity2, the focus is startactivity () and startactivityforresult () two methods.
1. No parameter for activity jump
Intent it = new intent (activity1.this, activity2.class );
Startactivity (it );
Method 1 switches directly from activity1 to activity2. activity1 and activity2 are the class names of window 1 and window 2. Note: These two activities must be registered in androidmanifest. xml before they can be opened.
2. pass data to the next activity (use bundle and intent. putextras)
Intent it = new intent (activity1.this, activity2.class );
Bundle bundle = new bundle ();
Bundle. putstring ("name", "this is from mainactivity! ");
It. putextras ("BD", bundle); // it. putextra ("test", "shuju ");
Startactivity (it); // startactivityforresult (it, request_code );
Method 2 is similar to method 1, but implements data transmission. Note that the bundle object is similar to hashtable and can store a key and the corresponding object. However, intent objects can also store bundle objects as key-value pairs to implement
Data is transmitted between acitivty2. In activity2, you can use the following methods to obtain the data passed in activity1.
Intent intent = getintent ();
Bundle BD = intent. getbundleextra ("BD"); // obtain the corresponding object based on the bundle key.
String name = BD. getstring ("name ");
3. Return the result to the previous activity in activity2 (use setresult to start the activity for startactivityforresult (it, request_code)
Intent intent = getintent ();
Bundle bundle2 = new bundle ();
Bundle2.putstring ("name", "this is from showmsg! ");
Intent. putextras (bundle2 );
Setresult (result_ OK, intent );
4. If activity1 needs to be processed according to the parameters passed in activity2Onactivityresult Processing
@ Override protected void onactivityresult (INT requestcode, int resultcode, intent data ){
// Todo auto-generated method stub
Super. onactivityresult (requestcode, resultcode, data );
If (requestcode = request_code ){
If (resultcode = result_canceled)
Settitle ("cancle ");
Else if (resultcode = result_ OK ){
String temp = NULL;
Bundle bundle = data. getextras ();
If (bundle! = NULL) temp = bundle. getstring ("name ");
Settitle (temp );
}
}
}
Iii. Implicit intent and runtime binding
Implicit intent is a mechanism that allows anonymous application components to request service actions. When creating a new implicit intent, you specify the action to be executed as an option. You can provide the data required for this action.
When you use this new implicit intent to start the activity, Android will parse it at runtime and find the class that best suits to execute the action on the specified data type. This means that you can create projects that use other applications without having to know exactly which application you will borrow in advance.
For example, if you want a user to make a call in an application, instead of implementing a new dialing, it is better to use an implicit intent to request a call number (represented by a URI) as shown in the following code snippet:
If (somethingweird & itdontlookgood)
{
Intent intent = new intent (intent. action_dial, Uri. parse ("Tel: 555-2368 "));
Startactivity (intent );
}
Android parses this intent and starts an activity that provides the ability to perform a dial-up action on a number. Here, it is a dial-up activity.
The following are some other intent usage examples (from javaeye)
Display webpage
Uri uri = URI. parse ("http://google.com /");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
Show Map
Uri uri = URI. parse ("Geo: 38.899533,-77.036476 ");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
// Other geo uri examples
// GEO: latitude, longpolling
// GEO: latitude, longpolling? Z = zoom
// GEO: 0, 0? Q = My + street + address
// GEO: 0, 0? Q = business + near + city
// Google. Streetview: cbll = Lat, LNG & white = 1, yaw, pitch, zoom & MZ = mapzoom
Route Planning
Uri uri = URI. parse ("http://maps.google.com/maps? F = D & saddr = startlat % 20 startlng & daddr = endlat % 20 endlng & HL = EN ");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
// Where startlat, startlng, endlat, endlng are a long with 6 decimals like: 50.123456
Call
// Call the dialing program
Uri uri = URI. parse ("Tel: 0800000123 ");
Intent it = new intent (intent. action_dial, Uri );
Startactivity (it );
// Directly call out
Uri uri = URI. parse ("Tel: 0800000123 ");
Intent it = new intent (intent. action_call, Uri );
Startactivity (it );
// Add this parameter to androidmanifest. xml
// <Uses-Permission id = "android. Permission. call_phone"/>
Send SMS/MMS
// Call the SMS Program
Intent it = new intent (intent. action_view, Uri );
It. putextra ("sms_body", "the SMS text ");
It. settype ("Vnd. Android-DIR/MMS-SMS ");
Startactivity (it );
// Send messages
Uri uri = URI. parse ("smsto: // 0800000123 ");
Intent it = new intent (intent. action_sendto, Uri );
It. putextra ("sms_body", "the SMS text ");
Startactivity (it );
// Send MMS
Uri uri = URI. parse ("content: // media/external/images/Media/23 ");
Intent it = new intent (intent. action_send );
It. putextra ("sms_body", "some text ");
It. putextra (intent. extra_stream, Uri );
It. settype ("image/PNG ");
Startactivity (it );
Send email
Uri uri = URI. parse ("mailto: xxx@abc.com ");
Intent it = new intent (intent. action_sendto, Uri );
Startactivity (it );
Intent it = new intent (intent. action_send );
It. putextra (intent. extra_email, "me@abc.com ");
It. putextra (intent. extra_text, "the email body text ");
It. settype ("text/plain ");
Startactivity (intent. createchooser (IT, "Choose email client "));
Intent it = new intent (intent. action_send );
String [] TOS = {"me@abc.com "};
String [] CCS = {"you@abc.com "};
It. putextra (intent. extra_email, TOS );
It. putextra (intent. extra_cc, CCS );
It. putextra (intent. extra_text, "the email body text ");
It. putextra (intent. extra_subject, "the email subject text ");
It. settype ("message/rfc822 ");
Startactivity (intent. createchooser (IT, "Choose email client "));
// Transfer the attachment
Intent it = new intent (intent. action_send );
It. putextra (intent. extra_subject, "the email subject text ");
It. putextra (intent. extra_stream, "http://www.cnblogs.com/l_dragon/admin/file:///sdcard/mysong.mp3 ");
Sendintent. settype ("audio/MP3 ");
Startactivity (intent. createchooser (IT, "Choose email client "));
Play multimedia
Uri uri = URI. parse ("http://www.cnblogs.com/l_dragon/admin/file:///sdcard/song.mp3 ");
Intent it = new intent (intent. action_view, Uri );
It. settype ("audio/MP3 ");
Startactivity (it );
Uri uri = URI. withappendedpath (mediastore. Audio. Media. internal_content_uri, "1 ");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
Market Problems
// Search for an application
Uri uri = URI. parse ("Market: // search? Q = pname: pkg_name ");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
// Where pkg_name is the full package path for an application
// Display information about an application
Uri uri = URI. parse ("Market: // details? Id = app_id ");
Intent it = new intent (intent. action_view, Uri );
Startactivity (it );
// Where app_id is the Application ID, find the ID
// By clicking on your application on market Home
// Page, and notice the ID from the address bar
Uninstall Application
Uri uri = URI. fromparts ("package", strpackagename, null );
Intent it = new intent (intent. action_delete, Uri );
Startactivity (it );