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

 

Hầu hết các ngôn ngữ lập trình hỗ trợ khái niệm IPC method-calling (gọi phương thức giữa các tiến trình khác nhau) với các tham số truyền theo hai chiều giữa phương thức gọi (caller) và phương thức bị gọi (callee – invoked method).

 

Ở Android, một activity phát lệnh gọi một activity khác bằng cách sử dụng một đối tượng Intent.

 

Lưu ý: Trong Android, caller không dừng lại đợi activity được gọi trả về kết quả. Thay vào đó, nên dùng một phương thức nghe (listening-method) [onActivityResult(.) ].

 

pptx28 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 1979 | Lượt tải: 5download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 12: Android Intents (Part 2), để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
l2", v2); // attach the container to the intent myIntentA1A2.putExtras(myData); // call Activity2, tell your local listener to wait response startActivityForResult(myIntentA1A2, 101); }//onClick }); }//onCreate ///////////////////////////////////////////////////////////////////// local listener receiving callbacks from other activities @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try{ if ((requestCode == 101 ) && (resultCode == Activity.RESULT_OK)){ Bundle myResults = data.getExtras(); Double vresult = myResults.getDouble("vresult"); lblResult.setText("Sum is " + vresult); } } catch (Exception e) { lblResult.setText("Oops! - " + requestCode + " " + resultCode); } }//onActivityResult }//Activity1 14 12. Android – Intents – Part 2 Tutorial. Activity Exchange 14 Step4. Activity2. Được gọi từ Activity1. Lấy input từ đối tượng bundle gắn với intent. Thực hiện tính toán tại chỗ. Đưa kết quả vào bundle. Trả về OK signal. package cis493.matos.intents6; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Activity2 extends Activity implements OnClickListener{ EditText dataReceived; Button btnDone; @Override protected void onsuper.onCreate(savedInstanceState); setContentView(R.layout.main2); dataReceived = (EditText) findViewById(R.id.etDataReceived); btnDone = (Button) findViewById(R.id.btnDone); btnDone.setOnClickListener(this); Create(Bundle savedInstanceState) { // pick call made to Activity2 via Intent Intent myLocalIntent = getIntent(); // look into the bundle sent to Activity2 for data items Bundle myBundle = myLocalIntent.getExtras(); Double v1 = myBundle.getDouble("val1"); Double v2 = myBundle.getDouble("val2"); // operate on the input data Double vResult = v1 + v2; // for illustration purposes. show data received & result dataReceived.setText("Data received is \n" + "val1= " + v1 + "\nval2= " + v2 + "\n\nresult= " + vResult); // add to the bundle the computed result myBundle.putDouble("vresult", vResult); // attach updated bumble to invoking intent myLocalIntent.putExtras(myBundle); // return sending an OK signal to calling activity setResult(Activity.RESULT_OK, myLocalIntent); }//onCreate @Override public void onClick(View v) { // close current screen - terminate Activity2 finish(); }//onClick }//Activity2 15 12. Android – Intents – Part 2 Tutorial. Activity Exchange 15 Step5. Update manifest của ứng dụng. Thêm thẻ mới cho “Activity2“ add 16 12. Android – Intents – Part 2 Intents 16 Example: Activity1 invokes Activity2 using an Intent. A bundle containg a set of values is sent back-and-forth between both activities. This example is similar to previous. You may want to skip it. //Activity1: Invoking a user-defined sub-activity //sending and receiving results from the sub-activity package cis493.intents;   import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*;   public class Activity1 extends Activity { 	TextView label1; 	TextView label1Returned; 	Button btnCallActivity2; 	private final int IPC_ID = 1122; 17 12. Android – Intents – Part 2 Intents 17 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities (see 12IntentDemo3.zip). 18 12. Android – Intents – Part 2 Intents 18 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. 	@Override 	public void onCreate(Bundle savedInstanceState) { 	super.onCreate(savedInstanceState); 	try { 	setContentView(R.layout.main); 	label1 = (TextView) findViewById(R.id.label1); 	label1Returned = (TextView) findViewById(R.id.label1Returned); 	btnCallActivity2 = (Button) findViewById(R.id.btnCallActivity2); 	btnCallActivity2.setOnClickListener(new Clicker1()); 	// for demonstration purposes- show in top label 	label1.setText("Activity1 (sending...) \n\n" 	+ "myString1: Hello Android" + "\n" 	+ "myDouble1: 3.141592 " + "\n" 	+ "myIntArray: {1 2 3} "); 	} catch (Exception e) { 	Toast.makeText(getBaseContext(), 	e.getMessage(), Toast.LENGTH_LONG).show(); 	} 	}// onCreate 19 12. Android – Intents – Part 2 Intents 19 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. 	 private class Clicker1 implements OnClickListener { 	@Override 	public void onClick(View v) { 	try { 	// create an Intent to talk to Activity2 	Intent myIntentA1A2 = new Intent(Activity1.this, Activity2.class); 	// prepare a Bundle and add the data pieces to be sent 	Bundle myData = new Bundle(); 	myData.putString("myString1", "Hello Android"); 	myData.putDouble("myDouble1", 3.141592); 	int[] myLittleArray = { 1, 2, 3 }; 	myData.putIntArray("myIntArray1", myLittleArray); 	// bind the Bundle and the Intent that talks to Activity2 	myIntentA1A2.putExtras(myData); 	// call Activity2 and wait for results 	startActivityForResult(myIntentA1A2, IPC_ID); 	} catch (Exception e) { 	Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show(); 	} 	}// onClick 	}// Clicker1 20 12. Android – Intents – Part 2 Intents 20 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. 	@Override 	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 	super.onActivityResult(requestCode, resultCode, data); 	try { 	switch (requestCode) { 	case IPC_ID: { 	//OK. This is the place to process the results sent back from the subactivity 	//see next slide 	} else { 	// user pressed the BACK button 	label1.setText("Selection CANCELLED!"); 	}// if 	break; 	}// case 	}// switch 	} catch (Exception e) { 	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 	}// try 	}// onActivityResult   }// AndroIntent1 21 12. Android – Intents – Part 2 Intents 21 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. 	@Override 	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 	super.onActivityResult(requestCode, resultCode, data); 	try { 	switch (requestCode) { 	case IPC_ID: { 	//OK. This is the place to process the results sent back from the sub-activity 	//see next slide 	} else { 	// user pressed the BACK button 	label1.setText("Selection CANCELLED!"); 	}// if 	break; 	}// case 	}// switch 	} catch (Exception e) { 	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 	}// try 	}// onActivityResult   }// AndroIntent1 22 12. Android – Intents – Part 2 Intents 22 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. 	// Activity2 is over - see what happened 	if (resultCode == Activity.RESULT_OK) {   	// good! - we have some data sent back from Activity2 	Bundle myReturnedData = data.getExtras(); 	String myReturnedString1 = myReturnedData.getString("myReturnedString1"); 	Double myReturnedDouble1 = myReturnedData.getDouble("myReturnedDouble1"); 	String myReturnedString2 = myReturnedData.getString("myCurrentTime"); 	// display in the bottom label 	label1Returned.setText(myReturnedString1 + "\n" 	+ Double.toString(myReturnedDouble1) + "\n" 	+ myReturnedString2); 	} 23 12. Android – Intents – Part 2 Intents 23 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. // Activity2. This subactivity receives a bundle of data, performs some work on the data and, // returns results to Activity1. package cis493.intents; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class Activity2 extends Activity { TextView label2; Button btnCallActivity1; 24 12. Android – Intents – Part 2 Intents 24 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. // Activity2 – cont… @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); //bind UI variables to Java code label2 = (TextView)findViewById(R.id.label2); btnCallActivity1 = (Button)findViewById(R.id.btnCallActivity1); btnCallActivity1.setOnClickListener(new Clicker1()); //create a local Intent handler – we have been called! Intent myLocalIntent = getIntent(); //grab the data package with all the pieces sent to us Bundle myBundle = myLocalIntent.getExtras(); //extract the individual data parts of the bundle String str1 = myBundle.getString("myString1"); double dob1 = myBundle.getDouble("myDouble1"); int[] arr1 = myBundle.getIntArray("myIntArray1"); 	//Activity2 – cont… //do something with the data here (for example...) String strArr = "{ "; int sumIntValues = 0; for (int i=0; i 26 12. Android – Intents – Part 2 Intents 26 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. Layout main.xml 27 12. Android – Intents – Part 2 Intents 27 Example: Activity1 invokes Activity2 using an Intent. A bundle conating a set of values is sent back-and-forth between both activities. Layout main2.xml 28 28 	 12. Android – Intents Intents 28 Questions ? Bài tập về nhà/Lab04 Tạo 1 ứng dụng kết hợp các activity đã tạo từ các bài tập tuần trước (Lab03, lab02)ứng dụng này có 2 nút bấm (hoặc menu) , một cho phép chạy activity chính của lab02, một cho phép chạy activity chính của lab03. Hạn nộp: hỏi thầy thực hành. 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 12 Android Intents (Part 2).pptx