Android intent usage
1. Start a new Activity
- Intent it = new Intent (Activity. Main. this, Activity2.class );
- StartActivity (it );
2. pass data to the next Activity (use Bundle and Intent. putExtras)
- Intent it = new Intent (Activity. Main. this, Activity2.class );
- Bundle bundle = new Bundle ();
- Bundle. putString ("name", "This is from MainActivity! ");
- It. putextras (bundle); // it. putextra ("test", "shuju ");
- Startactivity (it); // startactivityforresult (it, request_code );
You can use the following methods to obtain data:
- Bundle bundle = getintent (). getextras ();
- String name = bundle. getstring ("name ");
3. Return the result to the previous activity (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. Call back the result processing function (onactivityresult) of the previous activity)
- @ 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 );
- }
- }
- }
Display webpage:
1. Uri uri = URI. parse ("http://www.google.com ");
2. Intent it = new intent (intent. action_view, Uri );
3. startactivity (it );
Display map:
1. Uri uri = URI. parse ("Geo: 38.899533,-77.036476 ");
2. Intent it = new intent (intent. action_view, Uri );
3. startactivity (it );
Path Planning:
1. Uri uri = URI. parse ("http://maps.google.com/maps? F = D & saddr = startlat % 20 startlng & daddr = endlat % 20 endlng & HL = EN ");
2. Intent it = new intent (intent. action_view, Uri );
3. startactivity (it );
Call number:
Call the dialup Program
1. Uri uri = URI. parse ("Tel: xxxxxx ");
2. Intent it = new intent (intent. action_dial, Uri );
3. startactivity (it );
1. Uri uri = URI. parse ("Tel. xxxxxx ");
2. Intent it = new intent (intent. action_call, Uri );
3. To use this function, you must add <uses-Permission id = "android. Permission. call_phone"/> to the configuration file.
Send SMS/MMS
Send SMS by calling
Program
1. Intent it = new intent (intent. action_view );
2. it. putextra ("sms_body", "the SMS text ");
3. It. settype ("Vnd. Android-DIR/MMS-SMS ");
4. startactivity (it );
Send SMS
1. Uri uri = URI. parse ("smsto: 0800000123 ");
2. Intent it = new intent (intent. action_sendto, Uri );
3. It. putextra ("sms_body", "the SMS text ");
4. startactivity (it );
Send MMS
1. Uri uri = URI. parse ("content: // media/external/images/Media/23 ");
2. Intent it = new intent (intent. action_send );
3. it. putExtra ("sms_body", "some text ");
4. it. putExtra (Intent. EXTRA_STREAM, uri );
5. it. setType ("image/png ");
6. startActivity (it );
Send Email
1. Uri uri = Uri. parse ("mailto: xxx@abc.com ");
2. Intent it = new Intent (Intent. ACTION_SENDTO, uri );
3. startActivity (it );
1. Intent it = new Intent (Intent. ACTION_SEND );
2. it. putExtra (Intent. EXTRA_EMAIL, "me@abc.com ");
3. it. putExtra (Intent. EXTRA_TEXT, "The email body text ");
4. it. setType ("text/plain ");
5. startActivity (Intent. createChooser (it, "Choose Email Client "));
1. Intent it = new Intent (Intent. ACTION_SEND );
2. String [] tos = {"me@abc.com "};
3. String [] ccs = {"you@abc.com "};
4. it. putExtra (Intent. EXTRA_EMAIL, tos );
5. it. putExtra (Intent. EXTRA_CC, ccs );
6. it. putExtra (Intent. EXTRA_TEXT, "The email body text ");
7. it. putExtra (Intent. EXTRA_SUBJECT, "The email subject text ");
8. it. setType ("message/rfc822 ");
9. startActivity (Intent. createChooser (it, "Choose Email Client "));
Add attachment
1. Intent it = new Intent (Intent. ACTION_SEND );
2. it. putExtra (Intent. EXTRA_SUBJECT, "The email subject text ");
3. it. putExtra (Intent. EXTRA_STREAM, "file: // sdcard/mysong.mp3 ");
4. sendIntent. setType ("audio/mp3 ");
5. startActivity (Intent. createChooser (it, "Choose Email Client "));
1. // send the attachment
2. Intent it = new Intent (Intent. ACTION_SEND );
3. it. putExtra (Intent. EXTRA_SUBJECT, "The email subject text ");
4. it. putExtra (Intent. EXTRA_STREAM, "file: // sdcard/eoe.mp3 ");
5. sendIntent. setType ("audio/mp3 ");
6. startActivity (Intent. createChooser (it, "Choose Email Client "));
Play multimedia
1.
2. Intent it = new Intent (Intent. ACTION_VIEW );
3. Uri uri = Uri. parse ("file: // sdcard/song.mp3 ");
4. it. setDataAndType (uri, "audio/mp3 ");
5. startActivity (it );
1. Uri uri = Uri. withAppendedPath (MediaStore. Audio. Media. INTERNAL_CONTENT_URI, "1 ");
2. Intent it = new Intent (Intent. ACTION_VIEW, uri );
3. startActivity (it );
Uninstall program
1. Uri uri = Uri. fromParts ("package", strPackageName, null );
2. Intent it = new Intent (Intent. ACTION_DELETE, uri );
3. startActivity (it );
Uninstall apk
1. Uri uninstallUri = Uri. fromParts ("package", "xxx", null );
2.
3. returnIt = new Intent (Intent. ACTION_DELETE, uninstallUri );
Install apk
1. Intent intent = new Intent (Intent. ACTION_VIEW );
2. intent. setDataAndType (Uri. parse ("file: // sdcard/test.apk"), "application/vnd. android. package-archive ");
3.
4. startActivity (intent );
Play audio
1. Uri playUri = Uri. parse ("file: // sdcard/download/everything.mp3 ");
2.
3. returnIt = new Intent (Intent. ACTION_VIEW, playUri );
1. // search for Applications
2. Uri uri = Uri. parse ("market: // search? Q = pname: pkg_name ");
3. Intent it = new Intent (Intent. ACTION_VIEW, uri );
4. startActivity (it );
5. // where pkg_name is the full package path for an application
6.
7. // display the detailed page of the specified application (this does not seem to be supported and app_id cannot be found)
8. Uri uri = Uri. parse ("market: // details? Id = app_id ");
9. Intent it = new Intent (Intent. ACTION_VIEW, uri );
10. startActivity (it );
11. // where app_id is the application ID, find the ID
12. // by clicking on your application on Market home
13. // page, and notice the ID from the address bar