Lập trình Android tiếng Việt - Chapter 12: Android Intents

Android Activities
Một ứng dụng Android có thể bao gồm nhiều activity.

 

Một activity dùng phương thức setContentView(.) để hiện một (thông thường) giao diện người dùng mà từ đó có thể thực hiện các hành động.

 

Các activity tồn tại độc lập với nhau, nhưng chúng thường trao đổi dữ liệu và các action (hành động).

 

Thông thường, một activity được chỉ định là activity đầu tiên (main) cái sẽ hiện ra đầu tiên khi ứng dụng được bật.

 

Việc chuyển từ activity này sang activity khác được thực hiện bằng cách yêu cầu activity hiện hành thực thi một intent.

 

Các activity tương tác với nhau theo hình thức không đồng bộ.

 

pptx53 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 1989 | Lượt tải: 1download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 12: Android Intents, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
e: google.streetview:cbll=lat,lng&cbp=1, yaw,,pitch,zoom&mz=mapZoom Reference:  String geoCode = "google.streetview:cbll=41.5020952,-81.6789717&cbp=1,270,,45,1&mz=1"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoCode)); startActivity(intent); Modify the Manifest adding the following requests: 30 12. Android – Intents Launching the Music Player 30 More Examples: Using Standard Actions Launching the Music Player Reference:  //launch music player Intent myActivity2 = 	new Intent("android.intent.action.MUSIC_PLAYER"); startActivity(myActivity2); 31 12. Android – Intents Playing a song 31 More Examples: Using Standard Actions Playing a song stored in the SD card Reference:  // play song "amarcord.mp3" saved in the SD Intent myActivity2 = 	 new Intent(android.content.Intent.ACTION_VIEW); Uri data = Uri.parse("file:///sdcard/amarcord.mp3"); String type = "audio/mp3"; myActivity2.setDataAndType(data, type); startActivity(myActivity2); 32 12. Android – Intents Sending MMS 32 More Examples: Using Standard Actions Sending MMS Add picture #1 from SD to MMS Reference:  //send mms attach picture #1 to it Uri uri = Uri.parse("content://media/external/images/media/1"); myActivity2 = new Intent(Intent.ACTION_SEND); myActivity2.putExtra("address", "555-1234"); myActivity2.putExtra("sms_body", "some text message goes here"); myActivity2.putExtra(Intent.EXTRA_STREAM, uri); myActivity2.setType("image/png"); startActivity(myActivity2); 33 12. Android – Intents Sending Email 33 More Examples: Using Standard Actions Sending Email Reference:  // send email Uri uri = Uri.parse("mailto:v.matos@csuohio.edu"); Intent myActivity2 = new Intent(Intent.ACTION_SENDTO, uri); // you may skip the next two pieces [subject/text] myActivity2.putExtra(Intent.EXTRA_SUBJECT, 	 "subject goes here"); myActivity2.putExtra(Intent.EXTRA_TEXT, 	 "The email's body goes here"); startActivity(myActivity2); 34 12. Android – Intents Setting System 34 More Examples: Using Standard Actions Setting System Reference:  Intent intent = new Intent( 	 	android.provider.Settings.ACTION_SETTINGS); startActivity(intent); 35 12. Android – Intents Setting System Locale 35 More Examples: Using Standard Actions Setting System Locale: Language & Keyboard Reference:  Intent intent = new Intent(	 android.provider.Settings.ACTION_LOCALE_SETTINGS); startActivity(intent); 36 12. Android – Intents Starting Activities and Getting Results 36 Gọi activity và lấy kết quả Phương thức startActivity(Intent) dùng để bật một activity mới và nó sẽ được đặt trên đỉnh activity stack. Đôi khi, ta muốn lấy kết quả trả về từ activity được gọi (sub-activity) khi nó kết thúc. Ví dụ, ta bật một activity cho phép người dùng chọn một người từ danh sách contact; khi activity đó kết thúc, nó trả về người được chọn. 37 12. Android – Intents Starting Activities and Getting Results 37 Để có thể lấy kết quả trả về từ activity được gọi, ta dùng phương thức sau để gọi activity: 	startActivityForResult ( Intent, requestCodeID ) Trong đó, tham số thứ hai (requestCodeID) định danh lời gọi activity. Phương thức không đồng bộ sau lấy kết quả trả về từ sub-activity 	onActivityResult ( requestCodeID, resultCode, Intent ) 38 12. Android – Intents Starting Activities and Getting Results 38 Trước khi một activity kết thúc, nó có thể gọi setResult (resultCode) 	để gửi một tín hiệu kết thúc về cho nơi gọi nó (parent activity). Trong đó, result code có thể có giá trị chuẩn như 		Activity.RESULT_CANCELED, Activity.RESULT_OK, 	hoặc một giá trị tùy biến khác. Tất cả thông tin trên có thể được nhận bởi phương thức tại activity cha 	onActivityResult (int requestCodeID, int resultCode, Intent data) 	với ID mà activity cha đã cung cấp từ trước. Nếu một activity con thất bại vì lí do nào đó (chẳng hạn crash), activity cha sẽ nhận được một kết quả trả về với giá trị RESULT_CANCELED. 39 12. Android – Intents Starting Activities and Getting Results 39 Intent: {action + data + requestCodeID } requestCodeID resultCode optional data Activity-1 startActivityForResult … … __________________ onActivityResult() … … Activity-2 _________________ onResult() … … 40 12. Android – Intents Intents 40 Example2. Let’s play golf - Call for a tee-time. Show all contacts and pick a particular one (Intent.ACTION_PICK). For a successful interaction the main-activity accepts the returned URI identifying the person we want to call (content://contacts/people/n). ‘Nicely’ show the selected contact’s entry allowing calling, texting, emailing actions (Intent.ACTION_VIEW). User’s main Activity-1 Built-in Activity-2 (show contact list) Intent.ACTION_PICK Contact’s Uri Built-in Activity-3 (show selected contact) Intent.ACTION_VIEW Call Send text message Send email 41 12. Android – Intents Intents 41 Example2. Let’s play golf - Call for a tee-time. Cont. Intent.ACTION_PICK Intent.ACTION_VIEW Main Activity 42 12. Android – Intents Intents 42 Example2 (cont.) Let’s play golf - Call for a tee-time Place the call Selected contact’s URI Terminate the call 43 12. Android – Intents Intents 43 Example2. Calling a sub-activity, receiving results. //IntentDemo2_Intent: making a phone call //receiving results from a sub-activity package cis493.intents; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class IntentDemo2 extends Activity { TextView label1; EditText text1; Button btnCallActivity2; 44 12. Android – Intents Intents 44 Example2. Calling a sub-activity, receiving results. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { setContentView(R.layout.main); label1 = (TextView)findViewById(R.id.label1); text1 = (EditText)findViewById(R.id.text1); btnCallActivity2 = (Button)findViewById(R.id.btnPickContact); btnCallActivity2.setOnClickListener(new ClickHandler()); } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } }//onCreate 45 12. Android – Intents Intents 45 Example2. Calling a sub-activity, receiving results. private class ClickHandler implements OnClickListener { @Override public void onClick(View v) { try { // myData refer to: content://contacts/people/ String myData = text1.getText().toString(); //you may also try ACTION_VIEW instead Intent myActivity2 = new Intent(Intent.ACTION_PICK, Uri.parse(myData)); // start myActivity2. // Tell it that our requestCodeID (or nickname) is 222 startActivityForResult(myActivity2, 222); // Toast.makeText(getApplicationContext(), // "I can't wait for you", 1).show(); 	} catch (Exception e) { label1.setText(e.getMessage()); } }//onClick }//ClickHandler 46 12. Android – Intents Intents 46 Example2. Calling a sub-activity, receiving results. @Override protected void onActivityResult(int requestCode, 	 int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { // use requestCode to find out who is talking back to us switch (requestCode){ case (222): { // 222 is our friendly contact-picker activity if (resultCode == Activity.RESULT_OK) { String selectedContact = data.getDataString(); // it will return an URI that looks like: // content://contacts/people/n // where n is the selected contacts' ID label1.setText(selectedContact.toString()); //show a 'nice' screen with the selected contact Intent myAct3 = new Intent (Intent.ACTION_VIEW, Uri.parse(selectedContact)); startActivity(myAct3); } Listener 47 12. Android – Intents Intents 47 Example2. Calling a sub-activity, receiving results. else { //user pressed the BACK button label1.setText("Selection CANCELLED " 	 + requestCode + " " + resultCode); } break; } }//switch } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), 	 Toast.LENGTH_LONG).show(); } }// onActivityResult }//IntentDemo2 48 12. Android – Intents Intents 48 Example2. Calling a sub-activity, receiving results. 49 12. Android – Intents Intents 49 Example3. Showing Pictures and Video - Calling a sub-activity, receiving results. private void showSoundTracks() { 	Intent myIntent = new Intent(); 	myIntent.setType("video/*, images/*"); 	myIntent.setAction(Intent.ACTION_GET_CONTENT); 	startActivityForResult(myIntent, 0); }//showSoundTracks @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 	super.onActivityResult(requestCode, resultCode, intent); 	if ((requestCode == 0) && (resultCode == Activity.RESULT_OK)) { 	String selectedImage = intent.getDataString(); 	Toast.makeText(this, selectedImage, 1).show(); 	// show a 'nice' screen with the selected image 	Intent myAct3 = new Intent(Intent.ACTION_VIEW, Uri.parse(selectedImage)); 	startActivity(myAct3); 	} }//onActivityResult All videos and all still images 50 12. Android – Intents Intents 50 Example3. Showing Pictures and Video - Calling a sub-activity, receiving results. video 51 12. Android – Intents Intents 51 Example4. Showing/Playing Sound Tracks - Calling a sub-activity, receiving results. private void showSoundTracks() { 	Intent myIntent = new Intent(); 	myIntent.setType("audio/mp3"); 	myIntent.setAction(Intent.ACTION_GET_CONTENT); 	startActivityForResult(myIntent, 0); }//showSoundTracks The returned string value is similar to the following “content://media/external/audio/media/14” ACTION_VIEW on that Uri would produce a result similar to the image on the right 52 52 	 12. Android – Intents Intents 52 Questions ? 53 53 12. Android – Intents Intents 53 Built-in Standard Broadcast Actions List of standard actions that Intents can use for receiving broadcasts (usually through registerReceiver(BroadcastReceiver, IntentFilter) or a tag in a manifest). ACTION_TIME_TICK ACTION_TIME_CHANGED ACTION_TIMEZONE_CHANGED ACTION_BOOT_COMPLETED ACTION_PACKAGE_ADDED ACTION_PACKAGE_CHANGED ACTION_PACKAGE_REMOVED ACTION_UID_REMOVED ACTION_BATTERY_CHANGED 54 54 	 12. Android – Intents Intents 54 Appendix: Getting Permissions Becomes: 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 12 Android Intents.pptx
Tài liệu liên quan